Skip to main content

snippets_js

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;
});