2

I'm using Python to make requests to Pipefy GraphQL API. I already read the documentation and make search in pipefy forum, but I could not figure what is wrong with the query bellow:

pipeId = '171258'
query ="""
        {
            "query": "{allCards(pipeId: %s, first: 30, after: 'WyIxLjAiLCI1ODAuMCIsMzI0OTU0NF0'){pageInfo{endCursor hasNextPage}edges{node{id title}}}}"
        }
        """%(pipeid)

The query worked pretty well until I added the after parameter. I already tried variations like:

after: "WyIxLjAiLCI1ODAuMCIsMzI0OTU0NF0"

after: \"WyIxLjAiLCI1ODAuMCIsMzI0OTU0NF0\"

after: \n"WyIxLjAiLCI1ODAuMCIsMzI0OTU0NF0\n"

I know the issue is related with the escaping, because the API return messages like this:

'{"errors":[{"locations":[{"column":45,"line":1}],"message":"token recognition error at: \'\'\'"},{"locations":[{"column":77,"line":1}],"message":"token recognition error at: \'\'\'"}]}\n'

(this message is returned when the request is made with after: 'WyIxLjAiLCI1ODAuMCIsMzI0OTU0NF0')

Any help here would be immensely handful! Thanks

3 Answers 3

1

I had the same problem as you today (and saw your post on Pipefy's Support page). I personally entered in contact with Pipefy's developers but they weren't helpful at all.

I solved it by escaping the query correctly.

Try like this:

query = '{"query": "{ allCards(pipeId: %s, first: 30, after: \\"WyIxLjAiLCI1ODAuMCIsMzI0OTU0NF0\\"){ pageInfo{endCursor hasNextPage } edges { node { id title } } } }"}'

Using single quotes to define the string and double-backslashes before the doublequotes included in the cursor.

Sign up to request clarification or add additional context in comments.

1 Comment

Sure! Don't forget to upvote-it and mark it as answer, to help other to find it :))
1

With the code snippet below you are able to call the function get_card_list passing the authentication token (as String) and the pipe_id (as integer) and retrieve the whole card list of your pipe.

The get_card_list function will call the function request_card_list until the hasNextpage is set to False, updating the cursor in each call.

# Function responsible to get cards from a pipe using Pipefy's GraphQL API
def request_card_list(auth_token, pipe_id, hasNextPage=False, endCursor=""):    
    url = "https://api.pipefy.com/graphql"
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer %s' %auth_token
    }
    if not hasNextPage:
        payload = '{"query": "{ allCards(pipeId: %i, first: 50) { edges { node { id title  phases_history { phase { name } firstTimeIn lastTimeOut } } cursor } pageInfo { endCursor hasNextPage } } }"}' %pipe_id
    else:
        payload = '{"query": "{ allCards(pipeId: %i, first: 50, after: \\"%s\\") { edges { node { id title  phases_history { phase { name } firstTimeIn lastTimeOut } } cursor } pageInfo { endCursor hasNextPage } } }"}' % (pipe_id, endCursor)

    response = requests.request("POST", url, data=payload, headers=headers)
    response_body = response.text
    response_body_dict = json.loads(response_body)
    response_dict_list = response_body_dict['data']['allCards']['edges']

    card_list = []
    for d in response_dict_list:
        for h in d['node']['phases_history']:
            h['firstTimeIn'] = datetime.strptime(h['firstTimeIn'], date_format)
            if h['lastTimeOut']:
                h['lastTimeOut'] = datetime.strptime(h['lastTimeOut'], date_format)
        card_list.append(d['node'])

    return_list = [card_list, response_body_dict['data']['allCards']['pageInfo']['hasNextPage'], response_body_dict['data']['allCards']['pageInfo']['endCursor']]
    return return_list

# Function responsible to get all cards from a pipe using Pipefy's GraphQL API and pagination
def get_card_list(auth_token, pipe_id):
    card_list = []
    response = request_card_list(auth_token, pipe_id)
    card_list = card_list + response[0]

    while response[1]:
        response = request_card_list(auth_token, pipe_id, response[1], response[2])
        card_list = card_list + response[0]

    return(card_list)

Comments

0

Thanks for Lodi answer, I was able to do the next step. How to use a variable to pass the "after" parameter for the query As it was quite difficult I decide to share it here for those facing the same challenge.

end_cursor = 'WyIxLjAiLCI2NTcuMCIsNDgwNDA2OV0'
end_cursor = "\\" + "\"" + end_cursor  + "\\" + "\""
# desired output: end_cursor = '\"WyIxLjAiLCI2NTcuMCIsNDgwNDA2OV0\"'
query ="""
       {
        "query": "{allCards(pipeId: %s, first: 50, after: %s){pageInfo{endCursor hasNextPage}edges{node{id title}}}}"
        }
       """%(pipeid, end_cursor)

2 Comments

Sir, the correct way to continue getting help would be to create a new question. But, as I already have it solved in a code snippet, I will post as answer below.
Thanks Lodi, You helped me a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.