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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix location of a targeted entity #104

Merged
merged 1 commit into from
Mar 26, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions packages/github/__tests__/location.ts
Original file line number Diff line number Diff line change
@@ -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');
});
3 changes: 3 additions & 0 deletions packages/github/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const config = require('../../jest.config');

module.exports = config;
1 change: 1 addition & 0 deletions packages/github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions packages/github/src/location.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
GraphQLSchema,
printSchema,
buildSchema,
isObjectType,
isDirective,
isEnumType,
Expand All @@ -10,15 +11,20 @@ import {
getLocation as graphqlGetLocation,
} from 'graphql';

function normalizeSchema(schema: GraphQLSchema): GraphQLSchema {
return buildSchema(printSchema(schema));
}

export function getLocation({
path,
schema,
}: {
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) {
Expand Down
7 changes: 7 additions & 0 deletions packages/github/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": ["jest"]
},
"include": ["__tests__/**/*.ts"]
}