GraphQL
The GraphQL connector allows you to merge remote GraphQL APIs with your existing project API using the Edge Gateway.
name
(required) — The unique name for connectornamespace
(optional,true
by default) — Enable or disable the namespaceurl
— The URL used to execute requests and introspectionheaders
(optional) — The static or forwarded headers sent with requeststransforms
(optional) — Any schema transformations to be applied
import { config, connector, graph } from '@grafbase/sdk'
const g = graph.Standalone()
const contentful = connector.GraphQL('Contentful', {
url: g.env('CONTENTFUL_API_URL'),
headers: headers => {
headers.set('Authorization', `Bearer ${g.env('CONTENTFUL_API_KEY')}`)
},
})
g.datasource(contentful)
export default config({ graph: g })
The GraphQL connector provides transforms that allow you to modify the shape of the API exposed from Grafbase.
If you want to remove one or more fields from the API, you can use the
exclude
transform. This accepts one or more dot separated paths specifying
the fields you'd like to exclude. You can also use wildcards to select all the
fields of a given type.
import { config, connector, graph } from '@grafbase/sdk'
const g = graph.Standalone()
const identity = connector.GraphQL('IdentityApi', {
url: g.env('IDENTITY_API_URL'),
transforms: schema => {
// Hide some sensitive fields of the User type
schema.exclude('User.password', 'User.secret_key')
// Hide all mutations
schema.exclude('Mutations.*')
},
})
g.datasource(identity)
export default config({ graph: g })
When using Grafbase to connect many different data sources together, there's a chance you'll end up with two different types with the same name. Grafbase uses type prefixes by default to avoid these: we take the remote type name and put a prefix on that name before we pull it into our API.
By default we use the name
of the data source for this. But it can be
controlled using the prefixTypes
transform:
import { config, connector, graph } from '@grafbase/sdk'
const g = graph.Standalone()
const contentful = connector.GraphQL('Contentful', {
url: g.env('CONTENTFUL_API_URL'),
transforms: schema => {
// Change the typePrefix to Cms
schema.prefixTypes('Cms')
},
})
g.datasource(contentful)
export default config({ graph: g })
This can also be used to remove the prefix:
import { config, connector, graph } from '@grafbase/sdk'
const g = graph.Standalone()
const contentful = connector.GraphQL('Contentful', {
url: g.env('CONTENTFUL_API_URL'),
transforms: schema => {
// Remove the prefix
schema.prefixTypes('')
},
})
g.datasource(contentful)
export default config({ graph: g })