Skip to main content

GraphQL

GraphQL is an open-source data query and manipulation language for APIs.

Official Websitehttps://graphql.org
Tagsgraphqlhttpapi
Network Connection needs

This integration needs network access to the server where the service is running.

See the Network access page for details about how to achieve that.

Credential configuration

To configure this credential, you need the url of the graphql server. Optionally, you can set some headers in the JSON field, which will be sent in all requests.

Here is an example of a filled credential configuration form in YepCode:

GraphQL Snippets available in Editor

note

The title is the triggering text for YepCode to autocomplete the script.

Integration

New integration from credential
const { gql } = require('graphql-request');
const graphQLClient = yepcode.integration.graphql('credential-slug')
New integration from plain authentication data
const { GraphQLClient, gql } = require('graphql-request');
const graphQLClient = new GraphQLClient('https://api.spacex.land/graphql/', {
headers: {}
});

Query

Query (async/await)
const { gql } = require('graphql-request');
try {
const data = await graphQLClient.request(
gql`
query launchesPast($limit: 10) {
launchesPast(limit: $limit) {
mission_name
}
}
`,
{ limit: 10 }
)
console.log(JSON.stringify(data, undefined, 2))
} catch (error) {
console.error(JSON.stringify(error, undefined, 2));
throw error;
};
Query (Promise)
const { gql } = require('graphql-request');
graphQLClient.request(
gql`
query launchesPast($limit: 10) {
launchesPast(limit: $limit) {
mission_name
}
}
`,
{ limit: 10 }
).then((data) => {
console.log(JSON.stringify(data, undefined, 2));
})
.catch((error) => {
console.error(JSON.stringify(error, undefined, 2));
throw error;
});

Query with Headers

Query with headers (async/await)
const { gql } = require('graphql-request');
try {
const data = await graphQLClient.request(
gql`
query launchesPast($limit: 10) {
launchesPast(limit: $limit) {
mission_name
}
}
`,
{ limit: 10 }
);
console.log(JSON.stringify(data, undefined, 2))
} catch (error) {
console.error(JSON.stringify(error, undefined, 2));
throw error
};
Query with headers (Promise)
const { gql } = require('graphql-request');
graphQLClient.request(
gql`
query launchesPast($limit: 10) {
launchesPast(limit: $limit) {
mission_name
}
}
`,
{ limit: 10 },
{ CustomHeader: "value" }
).then((data) => {
console.log(JSON.stringify(data, undefined, 2));
}).catch((error) => {
console.error(JSON.stringify(error, undefined, 2));
throw error;
});

Mutation

Mutation (async/await)
const { gql } = require('graphql-request');
try {
const data = await graphQLClient.request(
gql`
mutation insert_users($objects: [users_insert_input!]!) {
insert_users(objects: $objects) {
returning {
name
}
}
}
`,
{
objects: [
{
name: 'JohnLocke',
rocket: 'JohnLocke',
timestamp: '1990-12-31T23:59:59.999Z',
twitter: 'JohnLocke'
}
]
}
)
console.log(JSON.stringify(data, undefined, 2))
} catch (error) {
console.error(JSON.stringify(error, undefined, 2));
throw error
};
Mutation (Promise)
const { gql } = require('graphql-request');
graphQLClient.request(
gql`
mutation insert_users($objects: [users_insert_input!]!) {
insert_users(objects: $objects) {
returning {
name
}
}
}
`,
{
objects: [
{
name: 'JohnLocke',
rocket: 'JohnLocke',
timestamp: '1990-12-31T23:59:59.999Z',
twitter: 'JohnLocke'
}
]
}
).then((data) => {
console.log(JSON.stringify(data, undefined, 2));
}).catch((error) => {
console.error(JSON.stringify(error, undefined, 2));
throw error;
});