···
6
-
FragmentDefinitionNode,
10
-
GraphQLInterfaceType,
1
+
import { InlineFragmentNode, FragmentDefinitionNode } from 'graphql';
import { warn, invariant } from '../helpers/help';
import { getTypeCondition } from './node';
5
+
import { SchemaIntrospector, SchemaObject } from './schema';
···
const BUILTIN_FIELD_RE = /^__/;
export const isFieldNullable = (
26
-
schema: GraphQLSchema,
17
+
schema: SchemaIntrospector,
if (BUILTIN_FIELD_RE.test(fieldName)) return true;
const field = getField(schema, typename, fieldName);
32
-
return !!field && isNullableType(field.type);
23
+
return !!field && field.type.kind !== 'NON_NULL';
export const isListNullable = (
36
-
schema: GraphQLSchema,
27
+
schema: SchemaIntrospector,
const field = getField(schema, typename, fieldName);
if (!field) return false;
42
-
const ofType = isNonNullType(field.type) ? field.type.ofType : field.type;
43
-
return isListType(ofType) && isNullableType(ofType.ofType);
34
+
field.type.kind === 'NON_NULL' ? field.type.ofType : field.type;
35
+
return ofType.kind === 'LIST' && ofType.ofType.kind !== 'NON_NULL';
export const isFieldAvailableOnType = (
47
-
schema: GraphQLSchema,
39
+
schema: SchemaIntrospector,
···
export const isInterfaceOfType = (
56
-
schema: GraphQLSchema,
48
+
schema: SchemaIntrospector,
node: InlineFragmentNode | FragmentDefinitionNode,
if (!typename) return false;
const typeCondition = getTypeCondition(node);
if (!typeCondition || typename === typeCondition) return true;
64
-
const abstractType = schema.getType(typeCondition);
65
-
const objectType = schema.getType(typename);
67
-
if (abstractType instanceof GraphQLObjectType) {
68
-
return abstractType === objectType;
71
-
expectAbstractType(abstractType, typeCondition);
72
-
expectObjectType(objectType, typename);
73
-
return schema.isPossibleType(abstractType, objectType);
56
+
schema.types[typeCondition] &&
57
+
schema.types[typeCondition].kind === 'OBJECT'
59
+
return typeCondition === typename;
60
+
expectAbstractType(schema, typeCondition!);
61
+
expectObjectType(schema, typename!);
62
+
return schema.isSubType(typeCondition, typename);
77
-
schema: GraphQLSchema,
66
+
schema: SchemaIntrospector,
81
-
const object = schema.getType(typename);
82
-
expectObjectType(object, typename);
84
-
const field = object.getFields()[fieldName];
70
+
expectObjectType(schema, typename);
71
+
const object = schema.types[typename] as SchemaObject;
72
+
const field = object.fields[fieldName];
'Invalid field: The field `' +
···
101
-
function expectObjectType(
104
-
): asserts x is GraphQLObjectType {
89
+
function expectObjectType(schema: SchemaIntrospector, typename: string) {
106
-
x instanceof GraphQLObjectType,
91
+
schema.types[typename] && schema.types[typename].kind === 'OBJECT',
'Invalid Object type: The type `' +
'` is not an object in the defined schema, ' +
···
115
-
function expectAbstractType(
118
-
): asserts x is GraphQLAbstractType {
100
+
function expectAbstractType(schema: SchemaIntrospector, typename: string) {
120
-
x instanceof GraphQLInterfaceType || x instanceof GraphQLUnionType,
102
+
schema.types[typename] &&
103
+
(schema.types[typename].kind === 'INTERFACE' ||
104
+
schema.types[typename].kind === 'UNION'),
'Invalid Abstract type: The type `' +
'` is not an Interface or Union type in the defined schema, ' +
···
export function expectValidKeyingConfig(
130
-
schema: GraphQLSchema,
114
+
schema: SchemaIntrospector,
if (process.env.NODE_ENV !== 'production') {
134
-
const types = schema.getTypeMap();
for (const key in keys) {
119
+
if (!schema.types[key]) {
'Invalid Object type: The type `' +
···
export function expectValidUpdatesConfig(
149
-
schema: GraphQLSchema,
132
+
schema: SchemaIntrospector,
updates: Record<string, Record<string, UpdateResolver>>
if (process.env.NODE_ENV === 'production') {
156
-
const mutation = schema.getMutationType();
157
-
const subscription = schema.getSubscriptionType();
158
-
const mutationFields = mutation ? mutation.getFields() : {};
159
-
const subscriptionFields = subscription ? subscription.getFields() : {};
160
-
const givenMutations = (mutation && updates[mutation.name]) || {};
161
-
const givenSubscription = (subscription && updates[subscription.name]) || {};
163
-
for (const fieldName in givenMutations) {
164
-
if (mutationFields[fieldName] === undefined) {
166
-
'Invalid mutation field: `' +
168
-
'` is not in the defined schema, but the `updates.Mutation` option is referencing it.',
139
+
if (schema.mutation) {
140
+
const mutationFields = (schema.types[schema.mutation] as SchemaObject)
142
+
const givenMutations = updates[schema.mutation] || {};
143
+
for (const fieldName in givenMutations) {
144
+
if (mutationFields[fieldName] === undefined) {
146
+
'Invalid mutation field: `' +
148
+
'` is not in the defined schema, but the `updates.Mutation` option is referencing it.',
174
-
for (const fieldName in givenSubscription) {
175
-
if (subscriptionFields[fieldName] === undefined) {
177
-
'Invalid subscription field: `' +
179
-
'` is not in the defined schema, but the `updates.Subscription` option is referencing it.',
155
+
if (schema.subscription) {
156
+
const subscriptionFields = (schema.types[
157
+
schema.subscription
158
+
] as SchemaObject).fields;
159
+
const givenSubscription = updates[schema.subscription] || {};
160
+
for (const fieldName in givenSubscription) {
161
+
if (subscriptionFields[fieldName] === undefined) {
163
+
'Invalid subscription field: `' +
165
+
'` is not in the defined schema, but the `updates.Subscription` option is referencing it.',
···
export function expectValidResolversConfig(
194
-
schema: GraphQLSchema,
181
+
schema: SchemaIntrospector,
resolvers: ResolverConfig
if (process.env.NODE_ENV === 'production') {
201
-
const validTypes = schema.getTypeMap();
for (const key in resolvers) {
204
-
const queryType = schema.getQueryType();
206
-
const validQueries = queryType.getFields();
190
+
if (schema.query) {
191
+
const validQueries = (schema.types[schema.query] as SchemaObject)
for (const resolverQuery in resolvers.Query) {
if (!validQueries[resolverQuery]) {
warnAboutResolver('Query.' + resolverQuery);
···
warnAboutResolver('Query');
216
-
if (!validTypes[key]) {
202
+
if (!schema.types[key]) {
219
-
const validTypeProperties = (schema.getType(
221
-
) as GraphQLObjectType).getFields();
205
+
const validTypeProperties = (schema.types[key] as SchemaObject).fields;
for (const resolverProperty in resolvers[key]) {
if (!validTypeProperties[resolverProperty]) {
warnAboutResolver(key + '.' + resolverProperty);
···
export function expectValidOptimisticMutationsConfig(
233
-
schema: GraphQLSchema,
217
+
schema: SchemaIntrospector,
optimisticMutations: OptimisticMutationConfig
if (process.env.NODE_ENV === 'production') {
240
-
const validMutations = schema.getMutationType()
241
-
? (schema.getMutationType() as GraphQLObjectType).getFields()
244
-
for (const mutation in optimisticMutations) {
245
-
if (!validMutations[mutation]) {
247
-
`Invalid optimistic mutation field: \`${mutation}\` is not a mutation field in the defined schema, but the \`optimistic\` option is referencing it.`,
224
+
if (schema.mutation) {
225
+
const validMutations = (schema.types[schema.mutation] as SchemaObject)
227
+
for (const mutation in optimisticMutations) {
228
+
if (!validMutations[mutation]) {
230
+
`Invalid optimistic mutation field: \`${mutation}\` is not a mutation field in the defined schema, but the \`optimistic\` option is referencing it.`,