/
GraphQL
Using a GraphQL data source inside our GraphQL server
To view this content, buy the book! 😃🙏
Or if you’ve already purchased.
GraphQL
If there’s a GraphQL API that we want to use data from, we have a few options:
- If we want to include parts of the API’s schema in our schema:
- If it supports federation, we should use that. For example, FaunaDB is working on support, and some third-party services we use might have a GraphQL API that supports federation. And if we have control over the API (e.g., if it’s one of our services), we can add support for federation.
- We can use schema stitching if the API doesn’t support federation. But unless we want a significant part of the API’s schema, it may be easier to use one of the below methods instead.
- If we just want to use data from the API in our resolvers:
- Use
GraphQLDataSource
fromapollo-datasource-graphql
to create a data source class. Similarly toRESTDataSource
, we can define awillSendRequest
method that adds an authorization header to all requests. But in our data fetching methods, instead ofthis.get('path')
, we usethis.query(QUERY_DOCUMENT)
. - Use
graphql-request
in our resolvers to fetch data from the data source (similar to ourgithubStars
subscription where we fetch data from GitHub’s GraphQL API). Whilegraphql-request
is nice for extremely simple uses likegithubStars
, usuallyGraphQLDataSource
is a better choice, as it’s a data source class.
- Use