Skip to content

Feature Request: query both GraphQL and Rest endpoints with variables at same time#280

Description

@basicdays

Hello all 馃憢

Currently trying out a hybrid approach against Github's APIs using both their GraphQL and Rest APIs. Attempted to put together a test query to see if I can get this thing working before I start adopting it and ran into an issue in regards to querying both their graphql and rest queries at once. I'm just currently executing this via a Node script.

The following query will result in the error ApolloError: Variable $repositoryPage is declared by Viewer but not used:

const FULL_VIEWER_QUERY = gql`
	query Viewer(
		$repositoryLimit: Int = 10
		$repositoryCursor: String = null
		$repositoryPage: Int = 1
	) {
		viewer {
			id
			name
			repositories(first: $repositoryLimit, after: $repositoryCursor) {
				edges {
					node {
						id
						name
					}
				}
				pageInfo {
					endCursor
					hasNextPage
				}
			}
		}
		restViewer @rest(type: "RestUser", path: "user") {
			id
			name
			reposUrl @export(as: "reposUrl")
			repos(per_page: $repositoryLimit, page: $repositoryPage)
				@rest(
					type: "[RestRepository]"
					path: "{exportVariables.reposUrl}?{args}"
					endpoint: "nested"
				) {
				id
				name
			}
		}
	}
`;

If I instead query viewer and restViewer fields in separate GraphQL queries, then this error does not occur. I would expect that the act of including a top level GraphQL field would not cause variables only used within rest fields to error out.

For reference, here is the client configuration:

"use strict";
const {
	ApolloClient,
	InMemoryCache,
	createHttpLink,
	from,
} = require("@apollo/client/core");
const { RestLink } = require("apollo-link-rest");
const { default: fetch, Headers } = require("cross-fetch");

global.Headers = Headers;

/**
 * @param {string} ghToken
 */
function getGithubClient(ghToken) {
	return new ApolloClient({
		name: "github",
		link: from([
			new RestLink({
				endpoints: {
					nested: " ",
				},
				uri: "https://api.github.com/",
				headers: {
					"User-Agent": "hybrid-test",
					Accept: "application/vnd.github.v3.raw+json",
					Authorization: `Token ${ghToken}`,
				},
				customFetch: fetch,
			}),
			createHttpLink({
				uri: "https://api.github.com/graphql",
				headers: {
					Authorization: `Bearer ${ghToken}`,
				},
				fetch: fetch,
			}),
		]),
		cache: new InMemoryCache(),
	});
}
exports = module.exports = getGithubClient;

For the sake of easily replicating this, I've added a repository that reproduces this: https://github.com/basicdays/ghtest

Notes:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions