Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always send variables required for query #2867

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const typeDefs = gql`
id: ID!
name: String
username: String
birthDate: String
birthDate(locale: String): String
account: AccountType
}

Expand Down Expand Up @@ -62,6 +62,11 @@ export const resolvers: GraphQLResolverMap<any> = {
__resolveObject(object) {
return users.find(user => user.id === object.id);
},
birthDate(user, args) {
return args.locale
? new Date(user.birthDate).toLocaleDateString(args.locale)
: user.birthDate;
},
},
Mutation: {
login(_, args) {
Expand Down
69 changes: 69 additions & 0 deletions packages/apollo-gateway/src/__tests__/executeQueryPlan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,75 @@ describe('executeQueryPlan', () => {
`);
});

it('should include variables in non-root requests', async () => {
const query = gql`
query Test($locale: String) {
topReviews {
body
author {
name
birthDate(locale: $locale)
}
}
}
`;

const operationContext = buildOperationContext(schema, query);
const queryPlan = buildQueryPlan(operationContext);

const requestContext = buildRequestContext();
requestContext.request.variables = { locale: 'en-US' };

const response = await executeQueryPlan(
queryPlan,
serviceMap,
requestContext,
operationContext,
);

expect(response.data).toMatchInlineSnapshot(`
Object {
"topReviews": Array [
Object {
"author": Object {
"birthDate": "12/10/1815",
"name": "Ada Lovelace",
},
"body": "Love it!",
},
Object {
"author": Object {
"birthDate": "12/10/1815",
"name": "Ada Lovelace",
},
"body": "Too expensive.",
},
Object {
"author": Object {
"birthDate": "6/23/1912",
"name": "Alan Turing",
},
"body": "Could be better.",
},
Object {
"author": Object {
"birthDate": "6/23/1912",
"name": "Alan Turing",
},
"body": "Prefer something else.",
},
Object {
"author": Object {
"birthDate": "6/23/1912",
"name": "Alan Turing",
},
"body": "Wish I had read this before.",
},
],
}
`);
});

it('can execute an introspection query', async () => {
const operationContext = buildOperationContext(
schema,
Expand Down
28 changes: 16 additions & 12 deletions packages/apollo-gateway/src/executeQueryPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,20 @@ async function executeFetch<TContext>(
const entities = Array.isArray(results) ? results : [results];
if (entities.length < 1) return;

if (!fetch.requires) {
let variables = Object.create(null);
if (fetch.variableUsages) {
for (const { node, defaultValue } of fetch.variableUsages) {
const name = node.name.value;
const providedVariables = context.requestContext.request.variables;
if (providedVariables && providedVariables[name] !== 'undefined') {
variables[name] = providedVariables[name];
} else if (defaultValue) {
variables[name] = defaultValue;
}
let variables = Object.create(null);
if (fetch.variableUsages) {
for (const { node, defaultValue } of fetch.variableUsages) {
const name = node.name.value;
const providedVariables = context.requestContext.request.variables;
if (providedVariables && providedVariables[name] !== 'undefined') {
variables[name] = providedVariables[name];
} else if (defaultValue) {
variables[name] = defaultValue;
}
}
}

if (!fetch.requires) {
const dataReceivedFromService = await sendOperation(
context,
operationForRootFetch(fetch, operationType),
Expand All @@ -187,10 +187,14 @@ async function executeFetch<TContext>(
}
});

if ('representations' in variables) {
throw new Error(`Variables cannot contain key "representations"`);
}

const dataReceivedFromService = await sendOperation(
context,
operationForEntitiesFetch(fetch),
{ representations },
{ ...variables, representations },
);

if (!dataReceivedFromService) {
Expand Down