diff --git a/CHANGELOG.md b/CHANGELOG.md index cfa7584e13..5a8b9b4332 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### vNEXT +- **github**: Fix location of a targeted entity[PR #104](https://github.com/kamilkisiela/graphql-inspector/pull/104) + ### v1.14.0 - **core**: Fix how GraphQL Schema is fetched [PR #101](https://github.com/kamilkisiela/graphql-inspector/pull/101) diff --git a/packages/github/__tests__/location.ts b/packages/github/__tests__/location.ts new file mode 100644 index 0000000000..a60f836f5d --- /dev/null +++ b/packages/github/__tests__/location.ts @@ -0,0 +1,55 @@ +import {buildSchema, printSchema} from 'graphql'; +import {getLocation} from '../src/location'; +import {GraphQLSchema} from 'graphql'; + +const schema = buildSchema(/* GraphQL */ ` + type Query { + user(id: ID!): User + users: [User!] + } + + type User { + id: ID! + name: String! + } +`); + +function printedLine(schema: GraphQLSchema, line: number): string { + const printed = printSchema(schema); + return printed.split('\n')[line - 1]; +} + +test('location of a Type', async () => { + const {line} = getLocation({ + schema, + path: 'User', + }); + + expect(printedLine(schema, line)).toMatch('type User {'); +}); +test('location of a Type.Field', async () => { + const {line} = getLocation({ + schema, + path: 'User.id', + }); + + expect(printedLine(schema, line)).toMatch('id: ID!'); +}); + +test('location of a Type.Field.Arg', async () => { + const {line} = getLocation({ + schema, + path: 'Query.user.id', + }); + + expect(printedLine(schema, line)).toMatch('user(id: ID!): User'); +}); + +test('location of a RootType.Field', async () => { + const {line} = getLocation({ + schema, + path: 'Query.user', + }); + + expect(printedLine(schema, line)).toMatch('user(id: ID!): User'); +}); diff --git a/packages/github/jest.config.js b/packages/github/jest.config.js new file mode 100644 index 0000000000..82fb2be5a7 --- /dev/null +++ b/packages/github/jest.config.js @@ -0,0 +1,3 @@ +const config = require('../../jest.config'); + +module.exports = config; diff --git a/packages/github/package.json b/packages/github/package.json index 26ef532844..f459bcb050 100644 --- a/packages/github/package.json +++ b/packages/github/package.json @@ -25,6 +25,7 @@ "scripts": { "start": "probot run ./dist/index.js", "dev": "yarn build && yarn start", + "test": "jest", "build": "tsc", "clean": "rm -rf dist/", "deploy": "now --docker", diff --git a/packages/github/src/location.ts b/packages/github/src/location.ts index db36c3f228..01adb544f0 100644 --- a/packages/github/src/location.ts +++ b/packages/github/src/location.ts @@ -1,6 +1,7 @@ import { GraphQLSchema, printSchema, + buildSchema, isObjectType, isDirective, isEnumType, @@ -10,6 +11,10 @@ import { getLocation as graphqlGetLocation, } from 'graphql'; +function normalizeSchema(schema: GraphQLSchema): GraphQLSchema { + return buildSchema(printSchema(schema)); +} + export function getLocation({ path, schema, @@ -17,8 +22,9 @@ export function getLocation({ path: string; schema: GraphQLSchema; }) { - const printed = printSchema(schema); - const loc = getNodeLocation({path, schema}); + const normalizedSchema = normalizeSchema(schema); + const printed = printSchema(normalizedSchema); + const loc = getNodeLocation({path, schema: normalizedSchema}); const source = new Source(printed); if (!loc) { diff --git a/packages/github/tsconfig.test.json b/packages/github/tsconfig.test.json new file mode 100644 index 0000000000..969de871bc --- /dev/null +++ b/packages/github/tsconfig.test.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "types": ["jest"] + }, + "include": ["__tests__/**/*.ts"] +}