Skip to content

Commit 310c97f

Browse files
DAB0mBUrigo
authored andcommitted
Step 4.3: Add chat field to Query type
1 parent 5ab032e commit 310c97f

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

Diff for: schema/resolvers.ts

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const resolvers = {
2121
chats() {
2222
return chats;
2323
},
24+
25+
chat(root: any, { chatId }: any) {
26+
return chats.find(c => c.id === chatId);
27+
},
2428
},
2529
};
2630

Diff for: schema/typeDefs.graphql

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ type Chat {
1717

1818
type Query {
1919
chats: [Chat!]!
20+
chat(chatId: ID!): Chat
2021
}

Diff for: tests/queries/__snapshots__/getChat.test.ts.snap

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Query.chat should fetch specified chat 1`] = `
4+
Object {
5+
"chat": Object {
6+
"id": "1",
7+
"lastMessage": Object {
8+
"content": "You on your way?",
9+
"createdAt": "2018-12-31T07:20:00.000Z",
10+
"id": "1",
11+
},
12+
"name": "Ethan Gonzalez",
13+
"picture": "https://randomuser.me/api/portraits/thumb/men/1.jpg",
14+
},
15+
}
16+
`;

Diff for: tests/queries/getChat.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { createTestClient } from 'apollo-server-testing';
2+
import { ApolloServer, gql } from 'apollo-server-express';
3+
import schema from '../../schema';
4+
5+
describe('Query.chat', () => {
6+
it('should fetch specified chat', async () => {
7+
const server = new ApolloServer({ schema });
8+
9+
const { query } = createTestClient(server);
10+
11+
const res = await query({
12+
variables: { chatId: '1' },
13+
query: gql`
14+
query GetChat($chatId: ID!) {
15+
chat(chatId: $chatId) {
16+
id
17+
name
18+
picture
19+
lastMessage {
20+
id
21+
content
22+
createdAt
23+
}
24+
}
25+
}
26+
`,
27+
});
28+
29+
expect(res.data).toBeDefined();
30+
expect(res.errors).toBeUndefined();
31+
expect(res.data).toMatchSnapshot();
32+
});
33+
});

Diff for: types/apollo-server-testing.d.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
declare module 'apollo-server-testing' {
2+
import { ApolloServerBase } from 'apollo-server-core';
3+
import { print, DocumentNode } from 'graphql';
4+
import { GraphQLResponse } from 'graphql-extensions';
5+
6+
type StringOrAst = string | DocumentNode;
7+
8+
// A query must not come with a mutation (and vice versa).
9+
type Query<TVariables> = {
10+
query: StringOrAst;
11+
mutation?: undefined;
12+
variables?: TVariables;
13+
};
14+
15+
type Mutation<TVariables> = {
16+
mutation: StringOrAst;
17+
query?: undefined;
18+
variables?: TVariables;
19+
};
20+
21+
export const createTestClient: <TVariables>(
22+
server: ApolloServerBase
23+
) => {
24+
query: (query: Query<TVariables>) => Promise<GraphQLResponse>;
25+
mutate: (mutation: Mutation<TVariables>) => Promise<GraphQLResponse>;
26+
};
27+
}

0 commit comments

Comments
 (0)