Skip to content

Commit

Permalink
Merge pull request #2867 from hannesj/master
Browse files Browse the repository at this point in the history
Always send variables required for query
  • Loading branch information
James Baxley authored Jun 17, 2019
2 parents c883224 + 198270b commit ed84d86
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 13 deletions.
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

0 comments on commit ed84d86

Please sign in to comment.