GraphQL
GraphQL is an open-source data query and manipulation language for APIs.
Official Websitehttps://graphql.org
Tagsgraphqlhttpapi
- JavaScript
- Python
NodeJS packagehttps://www.npmjs.com/package/graphql-request
Version5.1.0
Documentationhttps://gql.readthedocs.io/en/stable/
Pypi packagehttps://pypi.org/project/gql/
Version3.5.0
Source Codehttps://github.com/graphql-python/gql
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.
- JavaScript
- Python
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;
});
Integration
New integration from credential
graphql_client = yepcode.integration.graphql('credential-slug')
New integration from plain authentication data
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
transport = AIOHTTPTransport(url='https://api.spacex.land/graphql/', headers={})
graphql_client = Client(transport=transport, fetch_schema_from_transport=True)
Query
from gql import gql
query = gql(
"""
query launchesPast($limit: 10) {
launchesPast(limit: $limit) {
mission_name
}
}
"""
)
try:
data = graphql_client.execute(query, variable_values={ 'limit': 10 })
print(data)
except Exception as err:
print(err)
raise
Query with Headers
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
transport = AIOHTTPTransport(
url='https://api.spacex.land/graphql/',
headers={ 'CustomHeader': 'value' }
)
graphql_client = Client(transport=transport, fetch_schema_from_transport=True)
query = gql(
"""
query launchesPast($limit: 10) {
launchesPast(limit: $limit) {
mission_name
}
}
"""
)
try:
data = graphql_client.execute(query, variable_values={ 'limit': 10 })
print(data)
except Exception as err:
print(err)
raise
Mutation
from gql import gql
query = gql(
"""
mutation insert_users($objects: [user_inser_input!]!) {
insert_users(objects: $objects) {
returning {
name
}
}
}
"""
)
variable_values = {
'objects': [
{
'name': 'JohnLocke',
'rocket': 'JohnLocke',
'timestamp': '1990-12-31T23:59:59.999Z',
'twitter': 'JohnLocke'
}
]
}
try:
data = graphql_client.execute(query, variable_values=variable_values)
print(data)
except Exception as err:
print(err)
raise