/
repro1571.test.ts
105 lines (95 loc) · 2.51 KB
/
repro1571.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { linkToExecutor } from '@graphql-tools/links';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { wrapSchema } from '@graphql-tools/wrap';
import { ApolloLink, Observable } from 'apollo-link';
import gql from 'graphql-tag';
import { stitchSchemas } from '../src/stitchSchemas';
import { ExecutionResult, GraphQLError, graphql } from 'graphql';
export const typeDefs = gql`
input LoginInput {
username: String
password: String
}
type LoginPayload {
accessToken: String!
}
type Query {
meh: Boolean
}
type Mutation {
login(input: LoginInput!): LoginPayload!
}
`;
const link = new ApolloLink(operation => {
return new Observable(observer => {
const responses: Record<string, ExecutionResult> = {
'whatever@goodpass': {
data: {
login: {
accessToken: 'at',
},
},
},
'whatever@wrongpass': {
errors: [
({
message: 'INVALID_CREDENTIALS',
path: ['login'],
} as unknown) as GraphQLError,
],
data: null,
},
};
const response = responses[`${operation.variables.username}@${operation.variables.password}`];
if (response) {
observer.next(response);
observer.complete();
} else {
observer.error(new Error('UNEXPECTED_ERROR'));
}
});
});
const authSchema = wrapSchema({
executor: linkToExecutor(link),
schema: makeExecutableSchema({ typeDefs }),
});
const stitchedSchema = stitchSchemas({
subschemas: [{ schema: authSchema }],
});
describe('Repro for issue #1571', () => {
it.each`
username | password | response
${'whatever'} | ${'goodpass'} | ${{
data: {
login: {
accessToken: 'at',
},
},
}}
${'whatever'} | ${'wrongpass'} | ${{
errors: [{
message: 'INVALID_CREDENTIALS',
path: ['login'],
}],
data: null,
}}
`(
'should return the expected response for $username@$password',
async ({ username, password, response }: { username: string; password: string; response: string }) => {
const stitchedResult = await graphql(
stitchedSchema,
`
mutation Login($username: String!, $password: String!) {
login(input: { username: $username, password: $password }) {
accessToken
}
}
`,
undefined,
undefined,
{ username, password },
);
expect(stitchedResult).toEqual(response);
},
);
});