Make API calls
With this functionality, you can use our GraphQL to make API calls
p.query(query, variables)
You can use p.query to make queries using the permissions and access rights of the currently authenticated user. To know more about our queries, visit our documentation.
Parameters
query
: a GraphQL query. It accepts queries with operation names, but only the ones with operation type being query. You can learn more about operation name use in GraphQL official documentation. Mutations are not accepted, for those you should use the p.mutation method instead.variables
(optional): queries with operation names can receive variables. You can learn more about them here.
If you want to test a query, you can use our interactive GraphQL playground.
Returns
It returns a Promise
that will contain the result of your API call. You can learn more about promises in MDN documentation page.
Once the promise is resolved, the result object may contain data
and error
information. You can check more about it here.
Example call
Simple call
// Returns the ID of the logged user
p.query('{ me { id } }').then(function(result) {
console.log(result) // { data: { me: { id: "123", __typename: "User" } } }
});
Call with operation name
// Returns the title of a card
const cardTitleQuery = `
query CardTitle {
card(id: 1234) {
title
}
}`;
p.query(cardTitleQuery).then(function(result) {
console.log(result) // { data: { card: { title: "My Card", __typename: "Card" } } }
});
Call with variables
// Returns the name of the current pipe
const pipeNameQuery = `
query PipeName($pipeId: ID!) {
pipe(id: $pipeId) {
name
}
}`;
// This is the pipeId that should be used in queries, not p.pipeId
const variables = { pipeId: p.app.pipeId };
p.query(pipeNameQuery, variables).then(function(result) {
console.log(result) // { data: { pipe: { name: "My Pipe", __typename: "Pipe" } } }
});
Call with error
// Returns an error
// It can be helpful to check those errors when making your implementation
const userIdQuery = 'me { id } }' // Query missing a curly bracket
p.query(userIdQuery).then(function(result) {
console.log(result) // { errors: "Error: GraphQLError: Syntax Error: Unexpected Name "me"" }
});
Call with error and data
// Returns the org name, but not its members, because the user doesn't have permission to see it
const orgNameAndMembersQuery = `
query OrgNameAndMembers($orgId: ID!) {
organization(id: $orgId) {
name
orgMembers {
nodes {
name
}
}
}
}`;
const variables = { orgId: p.organizationId };
p.query(orgNameAndMembersQuery, variables).then(function (result) {
console.log(result);
/*
{ data: { organization: { name: "My Org", orgMembers: null, __typename: "Organization" } },
errors: [ { message: "Permission denied" ... } ] }
*/
});
p.mutation(mutation, variables)
You can use p.mutation to make mutations using the permissions and access rights of the currently authenticated user.
Parameters
mutation
: a GraphQL mutation. It accepts mutations with operation names, but only the ones with operation type being mutation. You can learn more about operation name use in GraphQL official documentation. Queries are not accepted, for those you should use the p.queries method instead.variables
(optional): queries with operation names can receive variables. You can learn more about them here.
If you want to test a mutation, you can use our interactive GraphQL playground.
Returns
It returns a Promise
that will contain the result of your API call. You can learn more about promises in MDN documentation page.
Once the promise is resolved, the result object may contain data
and error
information. You can check more about it here.
Example call
Simple call
// Creates a card and returns its id
const createCardMutation = `
mutation {
createCard(input: {pipe_id: ${p.app.pipeId}, title: "Novo card"}) {
card {
id
}
}
}`;
p.mutation(createCardMutation).then(function (result) {
console.log(result);
/* { data: { createCard: { card: { id: "12", __typename: "Card" } },
"__typename": "CreateCardPayload" } */
});
Call with operation name
// Deletes a card and returns if the query was successful or not
const deleteCardMutation = `
mutation deleteCard {
deleteCard(input: { id: 1234 }) {
success
}
}`;
p.mutation(deleteCardMutation).then(function (result) {
console.log(result);
// { data: { deleteCard: { success: true, __typename: "DeleteCardPayload" } } }
});
Other calls
To learn how to use variables and the error object you can check the examples for queries here.
Updated 6 months ago