Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update schemaIsSubgraph to also support non nullable _Service.sdl #7274

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-pears-float.md
@@ -0,0 +1,5 @@
---
'@apollo/server': patch
---

Update schemaIsSubgraph to also support non nullable \_Service.sdl
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
122 changes: 122 additions & 0 deletions packages/server/src/__tests__/plugin/schemaIsSubgraph.test.ts
@@ -0,0 +1,122 @@
import { schemaIsSubgraph } from '../../plugin/schemaIsSubgraph';
import { describe, it, expect } from '@jest/globals';

import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLNonNull,
} from 'graphql';

describe('schemaIsSubgraph', () => {
it('returns false when there is no service field', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'QueryType',
fields: {
hello: {
type: GraphQLString,
},
},
}),
});

expect(schemaIsSubgraph(schema)).toBe(false);
});

it('returns false when the sdl field is a not string', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'QueryType',
fields: {
_service: {
type: new GraphQLObjectType({
name: '_Service',
fields: {
sdl: {
type: GraphQLInt,
},
},
}),
},
},
}),
});

expect(schemaIsSubgraph(schema)).toBe(false);
});

it('returns false when the sdl field is a scalar', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'QueryType',
fields: {
_service: {
type: new GraphQLObjectType({
name: '_Service',
fields: {
sdl: {
type: new GraphQLObjectType({
name: 'Scalar',
fields: {
value: {
type: GraphQLString,
},
},
}),
},
},
}),
},
},
}),
});

expect(schemaIsSubgraph(schema)).toBe(false);
});

it('returns true when the sdl field is a string', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'QueryType',
fields: {
_service: {
type: new GraphQLObjectType({
name: '_Service',
fields: {
sdl: {
type: GraphQLString,
},
},
}),
},
},
}),
});

expect(schemaIsSubgraph(schema)).toBe(true);
});

it('returns true when the sdl field is a non nullable string', async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'QueryType',
fields: {
_service: {
type: new GraphQLObjectType({
name: '_Service',
fields: {
sdl: {
type: new GraphQLNonNull(GraphQLString),
},
},
}),
},
},
}),
});

expect(schemaIsSubgraph(schema)).toBe(true);
});
});
13 changes: 11 additions & 2 deletions packages/server/src/plugin/schemaIsSubgraph.ts
@@ -1,4 +1,9 @@
import { GraphQLSchema, isObjectType, isScalarType } from 'graphql';
import {
GraphQLSchema,
isObjectType,
isScalarType,
isNonNullType,
} from 'graphql';

// Returns true if it appears that the schema was appears to be of a subgraph
// (eg, returned from @apollo/subgraph's buildSubgraphSchema). This strategy
Expand All @@ -24,7 +29,11 @@ export function schemaIsSubgraph(schema: GraphQLSchema): boolean {
if (!sdlField) {
return false;
}
const sdlFieldType = sdlField.type;

let sdlFieldType = sdlField.type;
if (isNonNullType(sdlFieldType)) {
sdlFieldType = sdlFieldType.ofType;
}
if (!isScalarType(sdlFieldType)) {
return false;
}
Expand Down