GraphQL utilities
Using npm
$ npm install --save @jumpn/utils-graphql
Using yarn
$ yarn add @jumpn/utils-graphql
type GqlErrorLocation = {|
line: number,
column: number
|};
type GqlError = {|
message: string,
locations?: Array<GqlErrorLocation>
|};
type GqlRequest<Variables: void | Object = void> = {|
operation: string,
variables?: Variables
|};
type GqlRequestCompat<Variables: void | Object = void> = {|
query: string,
variables?: Variables
|};
type GqlResponse<Data> = {|
data?: Data,
errors?: Array<GqlError>
|};
type GqlOperationType = "mutation" | "query" | "subscription";
Transforms an array of GqlError into a string.
gqlErrors
Array<GqlError>
const gqlRespose = {
errors: [
{message: "First Error", locations: [{column: 10, line: 2}]},
{message: "Second Error", locations: [{column: 2, line: 4}]}
]
}
const error = errorsToString(gqlRespose.errors);
// string with the following:
// First Error (2:10)
// Second Error (4:2)
Returns string
Returns the type (query, mutation, or subscription) of the given operation
operation
string
const operation = `
subscription userSubscription($userId: ID!) {
user(userId: $userId) {
id
name
}
}
`;
const operationType = getOperationType(operation);
console.log(operationType); // "subscription"
Returns GqlOperationType
Returns true if documentNode has a subscription or false otherwise
documentNode
DocumentNode
Returns boolean
Creates a GqlRequest using given GqlRequestCompat
gqlRequestCompat
GqlRequestCompat<Variables>gqlRequestCompat.query
gqlRequestCompat.variables
const query = `
query userQuery($userId: ID!) {
user(userId: $userId) {
id
email
}
}
`;
console.log(requestFromCompat({query, variables: {userId: 10}}));
// {operation: "...", variables: {userId: 10}}
Returns GqlRequest<Variables>
Creates a GqlRequest using given GqlRequestCompat
gqlRequest
GqlRequest<Variables>gqlRequest.operation
gqlRequest.variables
const operation = `
query userQuery($userId: ID!) {
user(userId: $userId) {
id
email
}
}
`;
console.log(requestToCompat({operation, variables: {userId: 10}}));
// {query: "...", variables: {userId: 10}}
Returns GqlRequestCompat<Variables>
MIT ©️ Jumpn Limited