Skip to content

Commit 7f5aa75

Browse files
committed
test: add test cases for issue 107
related graphql-compose#107
1 parent 734ed1c commit 7f5aa75

File tree

2 files changed

+208
-3
lines changed

2 files changed

+208
-3
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/* @flow */
2+
3+
import {
4+
GraphQLSchema,
5+
GraphQLString,
6+
GraphQLObjectType,
7+
GraphQLInt,
8+
GraphQLList,
9+
graphql,
10+
} from 'graphql';
11+
import { TypeComposer, GQC } from '../../';
12+
13+
const remoteSchema = new GraphQLSchema({
14+
query: new GraphQLObjectType({
15+
name: 'Query',
16+
fields: {
17+
users: {
18+
type: new GraphQLList(
19+
new GraphQLObjectType({
20+
name: 'User',
21+
fields: {
22+
name: { type: GraphQLString },
23+
age: { type: GraphQLInt },
24+
access: {
25+
type: new GraphQLObjectType({
26+
name: 'Access',
27+
fields: {
28+
msg: { type: GraphQLString },
29+
},
30+
}),
31+
resolve: source => ({
32+
msg: source.age >= 20 ? `allowed` : 'disallowed',
33+
}),
34+
},
35+
},
36+
})
37+
),
38+
resolve: () => [{ name: 'u1', age: 10 }, { name: 'u2', age: 20 }, { name: 'u3', age: 30 }],
39+
},
40+
},
41+
}),
42+
});
43+
44+
beforeEach(() => {
45+
GQC.clear();
46+
});
47+
48+
describe('github issue #107 merge Schema types on GQL', () => {
49+
it('get QueryTC from remote schema', () => {
50+
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
51+
expect(RemoteQueryTC).toBeInstanceOf(TypeComposer);
52+
expect(RemoteQueryTC.getTypeName()).toBe('Query');
53+
expect(RemoteQueryTC.getFieldNames()).toEqual(['users']);
54+
55+
// remoteMutationTC = TypeComposer.create(remoteSchema._mutationType);
56+
// remoteSubscriptionTC = TypeComposer.create(remoteSchema._subscriptionType);
57+
});
58+
59+
it('get nested TC from remote schema', () => {
60+
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
61+
const RemoteUserTC = RemoteQueryTC.get('users');
62+
expect(RemoteUserTC.getTypeName()).toEqual('User');
63+
64+
const RemoteAccessTC = RemoteQueryTC.get('users.access');
65+
expect(RemoteAccessTC.getTypeName()).toEqual('Access');
66+
});
67+
68+
it('schema stiching on Query', async () => {
69+
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
70+
71+
GQC.rootQuery().addFields({
72+
tag: {
73+
type: TypeComposer.create(`type Tag { id: Int, title: String}`),
74+
resolve: () => ({ id: 1, title: 'Some tag' }),
75+
},
76+
...RemoteQueryTC.getFields(),
77+
});
78+
79+
expect(GQC.rootQuery().getFieldNames()).toEqual(['tag', 'users']);
80+
81+
const schema = GQC.buildSchema();
82+
expect(
83+
await graphql(
84+
schema,
85+
`
86+
query {
87+
tag {
88+
id
89+
title
90+
}
91+
users {
92+
age
93+
}
94+
}
95+
`
96+
)
97+
).toEqual({
98+
data: { tag: { id: 1, title: 'Some tag' }, users: [{ age: 10 }, { age: 20 }, { age: 30 }] },
99+
});
100+
});
101+
102+
it('schema stiching on Query.remote', async () => {
103+
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
104+
105+
GQC.rootQuery().addFields({
106+
tag: {
107+
type: TypeComposer.create(`type Tag { id: Int, title: String}`),
108+
resolve: () => ({ id: 1, title: 'Some tag' }),
109+
},
110+
remote: {
111+
type: TypeComposer.create({
112+
name: 'RemoteSchema',
113+
fields: RemoteQueryTC.getFields(),
114+
}),
115+
resolve: () => ({}), // it's important to return something (not null/undefined)
116+
},
117+
});
118+
119+
expect(GQC.rootQuery().getFieldNames()).toEqual(['tag', 'remote']);
120+
121+
const schema = GQC.buildSchema();
122+
expect(
123+
await graphql(
124+
schema,
125+
`
126+
query {
127+
tag {
128+
id
129+
title
130+
}
131+
remote {
132+
users {
133+
age
134+
}
135+
}
136+
}
137+
`
138+
)
139+
).toEqual({
140+
data: {
141+
tag: { id: 1, title: 'Some tag' },
142+
remote: { users: [{ age: 10 }, { age: 20 }, { age: 30 }] },
143+
},
144+
});
145+
});
146+
147+
it('using remote type in local schema', async () => {
148+
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
149+
const RemoteUserTC = RemoteQueryTC.getFieldTC('users');
150+
const remoteUsersFC = RemoteQueryTC.getField('users');
151+
152+
const LocalArticleTC = TypeComposer.create({
153+
name: 'Article',
154+
fields: {
155+
text: {
156+
type: 'String',
157+
},
158+
author: {
159+
type: RemoteUserTC,
160+
args: remoteUsersFC.args || {},
161+
resolve: (source, args, context, info) => {
162+
const users = remoteUsersFC.resolve(source, args, context, info);
163+
// for simplicity return first user
164+
return users[0];
165+
},
166+
},
167+
},
168+
});
169+
170+
GQC.rootQuery().addFields({
171+
article: {
172+
type: LocalArticleTC,
173+
resolve: () => ({ text: 'Article 1' }),
174+
},
175+
});
176+
177+
const schema = GQC.buildSchema();
178+
expect(
179+
await graphql(
180+
schema,
181+
`
182+
query {
183+
article {
184+
text
185+
author {
186+
name
187+
age
188+
access {
189+
msg
190+
}
191+
}
192+
}
193+
}
194+
`
195+
)
196+
).toEqual({
197+
data: {
198+
article: {
199+
text: 'Article 1',
200+
author: { access: { msg: 'disallowed' }, age: 10, name: 'u1' },
201+
},
202+
},
203+
});
204+
});
205+
});

src/__tests__/github-issues-test.js renamed to src/__tests__/github_issues/72-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* @flow */
22

3-
import TypeComposer from '../typeComposer';
3+
import TypeComposer from '../../typeComposer';
44

5-
describe('github issues checks', () => {
6-
it('#72 extendField after addRelation', () => {
5+
describe('github issue #72', () => {
6+
it('extendField after addRelation', () => {
77
const MyTypeTC = TypeComposer.create(`type MyType { name: String }`);
88
const OtherTypeTC = TypeComposer.create(`type OtherType { name: String }`);
99
OtherTypeTC.addResolver({

0 commit comments

Comments
 (0)