easygraphql-parser
Advanced tools
Comparing version
'use strict' | ||
const { parse, Kind, printSchema, buildClientSchema } = require('graphql') | ||
const { parse, Kind, printSchema, buildClientSchema, GraphQLSchema } = require('graphql') | ||
const mergeWith = require('lodash.mergewith') | ||
@@ -21,3 +21,3 @@ | ||
opTypes[operation] = { | ||
type: convertType(operationType.kind), | ||
type: operationType.kind, | ||
operation, | ||
@@ -121,4 +121,5 @@ field: typeName | ||
const fieldType = findType(arg.type) | ||
const isDeprecated = validateIfDeprecated(arg.directives) | ||
return Object.assign({ name }, fieldType) | ||
return Object.assign({ name, isDeprecated }, fieldType) | ||
}) | ||
@@ -128,16 +129,12 @@ } | ||
/** | ||
* Convert the Schema type | ||
* @param type - Can be InputObjectTypeDefinition (used to receive values) or ObjectTypeDefinition (as response) | ||
* @returns {string} | ||
* Check if a field is deprecated | ||
* @param directives - Receive the directives array | ||
* @returns {boolean} | ||
*/ | ||
function convertType (type) { | ||
switch (type) { | ||
case Kind.INPUT_OBJECT_TYPE_DEFINITION: | ||
return 'InputType' | ||
case Kind.OBJECT_TYPE_DEFINITION: | ||
return 'ObjectType' | ||
function validateIfDeprecated (directives) { | ||
if (!directives.length) { | ||
return false | ||
} | ||
default: | ||
return type | ||
} | ||
return directives.some(directive => directive.name.value === 'deprecated') | ||
} | ||
@@ -151,3 +148,3 @@ | ||
const parsedType = { | ||
type: convertType(type.kind), | ||
type: type.kind, | ||
description: type.description, | ||
@@ -169,3 +166,4 @@ fields: [], | ||
const newField = Object.assign({ name, arguments: typeArguments }, fieldType) | ||
const isDeprecated = validateIfDeprecated(field.directives) | ||
const newField = Object.assign({ name, arguments: typeArguments, isDeprecated }, fieldType) | ||
fields.push(newField) | ||
@@ -199,3 +197,2 @@ }) | ||
const schema = schemaBuilder(parse(sourceVal)) | ||
// console.log('sss', schema.Query); | ||
mergeWith(result, schema, mergeNestedFields) | ||
@@ -207,6 +204,7 @@ }) | ||
if (typeof source === 'object') { | ||
if (source.data) { | ||
source = printSchema(buildClientSchema(source.data)) | ||
if (source instanceof GraphQLSchema) { | ||
source = printSchema(source) | ||
} else { | ||
source = printSchema(source) | ||
source = source.data ? source.data : source | ||
source = printSchema(buildClientSchema(source)) | ||
} | ||
@@ -213,0 +211,0 @@ } |
{ | ||
"name": "easygraphql-parser", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Parse GraphQL Schema", | ||
@@ -41,2 +41,5 @@ "main": "index.js", | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^0.13.0 || ^14.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -43,0 +46,0 @@ "chai": "^4.1.2", |
@@ -62,3 +62,4 @@ <h1 align="center"> | ||
noNullArrayValues: false | ||
type: 'String' | ||
type: 'String', | ||
isDeprecated: false | ||
}, | ||
@@ -71,3 +72,4 @@ { | ||
noNullArrayValues: true | ||
type: 'Int' | ||
type: 'Int', | ||
isDeprecated: false | ||
} | ||
@@ -87,3 +89,4 @@ ], | ||
noNullArrayValues: false | ||
type: 'Family' | ||
type: 'Family', | ||
isDeprecated: false | ||
} | ||
@@ -90,0 +93,0 @@ ], |
@@ -9,2 +9,3 @@ /* eslint-env mocha */ | ||
const { expect } = require('chai') | ||
const { Kind } = require('graphql') | ||
const schemaParser = require('../lib/schemaParser') | ||
@@ -37,3 +38,3 @@ | ||
expect(schema.Me).to.exist | ||
expect(schema.Me.type).to.be.eq('ObjectType') | ||
expect(schema.Me.type).to.be.eq(Kind.OBJECT_TYPE_DEFINITION) | ||
expect(schema.Me.description).to.be.eq(undefined) | ||
@@ -45,8 +46,9 @@ expect(schema.Me.fields.length).to.be.gt(0) | ||
it('Schema should have the properties with the null type and array type', () => { | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'id', type: 'ID', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'email', type: 'String', noNull: false, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'username', type: 'String', noNull: true, isArray: true, arguments: [], noNullArrayValues: true }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'fullName', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'phone', type: 'Int', noNull: true, isArray: true, arguments: [], noNullArrayValues: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'apiKey', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'id', type: 'ID', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'email', type: 'String', noNull: false, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'username', type: 'String', noNull: true, isArray: true, arguments: [], noNullArrayValues: true, isDeprecated: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'fullName', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'phone', type: 'Int', noNull: true, isArray: true, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'apiKey', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.Me.fields).to.have.deep.include({ name: 'result', type: 'Float', noNull: false, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: true }) | ||
}) | ||
@@ -58,3 +60,3 @@ }) | ||
expect(schema.User).to.exist | ||
expect(schema.User.type).to.be.eq('ObjectType') | ||
expect(schema.User.type).to.be.eq(Kind.OBJECT_TYPE_DEFINITION) | ||
expect(schema.User.description).to.be.eq(undefined) | ||
@@ -66,6 +68,6 @@ expect(schema.User.fields.length).to.be.gt(0) | ||
it('Schema should have the properties with the null type and array type', () => { | ||
expect(schema.User.fields).to.have.deep.include({ name: 'email', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'username', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'fullName', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'phone', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'email', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'username', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'fullName', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'phone', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
}) | ||
@@ -77,3 +79,3 @@ }) | ||
expect(schema.Query).to.exist | ||
expect(schema.Query.type).to.be.eq('ObjectType') | ||
expect(schema.Query.type).to.be.eq(Kind.OBJECT_TYPE_DEFINITION) | ||
expect(schema.Query.description).to.be.eq(undefined) | ||
@@ -85,6 +87,9 @@ expect(schema.Query.fields.length).to.be.gt(0) | ||
it('Schema should have the properties with the null type and array type', () => { | ||
const getUserByUsernameArguments = [{ name: 'username', noNull: true, isArray: false, type: 'String', noNullArrayValues: false }, { name: 'id', noNull: true, isArray: false, type: 'Int', noNullArrayValues: false }] | ||
expect(schema.Query.fields).to.have.deep.include({ name: 'getMe', type: 'Me', noNull: false, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.Query.fields).to.have.deep.include({ name: 'getUsers', type: 'User', noNull: true, isArray: true, arguments: [], noNullArrayValues: true }) | ||
expect(schema.Query.fields).to.have.deep.include({ name: 'getUserByUsername', type: 'User', noNull: false, isArray: false, arguments: getUserByUsernameArguments, noNullArrayValues: false }) | ||
const getUserByUsernameArguments = [ | ||
{ name: 'username', noNull: true, isArray: false, type: 'String', noNullArrayValues: false, isDeprecated: false }, | ||
{ name: 'id', noNull: true, isArray: false, type: 'Int', noNullArrayValues: false, isDeprecated: false } | ||
] | ||
expect(schema.Query.fields).to.have.deep.include({ name: 'getMe', type: 'Me', noNull: false, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.Query.fields).to.have.deep.include({ name: 'getUsers', type: 'User', noNull: true, isArray: true, arguments: [], noNullArrayValues: true, isDeprecated: false }) | ||
expect(schema.Query.fields).to.have.deep.include({ name: 'getUserByUsername', type: 'User', noNull: false, isArray: false, arguments: getUserByUsernameArguments, noNullArrayValues: false, isDeprecated: false }) | ||
}) | ||
@@ -91,0 +96,0 @@ }) |
@@ -6,2 +6,3 @@ /* eslint-env mocha */ | ||
const { Kind } = require('graphql') | ||
const { expect } = require('chai') | ||
@@ -29,3 +30,3 @@ const schemaParser = require('../lib/schemaParser') | ||
expect(schema.RootQuery).to.exist | ||
expect(schema.RootQuery.type).to.be.eq('ObjectType') | ||
expect(schema.RootQuery.type).to.be.eq(Kind.OBJECT_TYPE_DEFINITION) | ||
expect(schema.RootQuery.description).to.be.eq(undefined) | ||
@@ -37,3 +38,3 @@ expect(schema.RootQuery.fields.length).to.be.gt(0) | ||
it('Schema should have the properties with the null type and array type', () => { | ||
expect(schema.RootQuery.fields).to.have.deep.include({ name: 'getUser', type: 'User', noNull: false, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.RootQuery.fields).to.have.deep.include({ name: 'getUser', type: 'User', noNull: false, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
}) | ||
@@ -45,3 +46,3 @@ }) | ||
expect(schema.RootMutation).to.exist | ||
expect(schema.RootMutation.type).to.be.eq('ObjectType') | ||
expect(schema.RootMutation.type).to.be.eq(Kind.OBJECT_TYPE_DEFINITION) | ||
expect(schema.RootMutation.description).to.be.eq(undefined) | ||
@@ -53,4 +54,4 @@ expect(schema.RootMutation.fields.length).to.be.gt(0) | ||
it('Schema should have the properties with the null type and array type', () => { | ||
const args = [{ name: 'input', noNull: true, isArray: false, type: 'UserInput', noNullArrayValues: false }] | ||
expect(schema.RootMutation.fields).to.have.deep.include({ name: 'createUser', type: 'User', noNull: false, isArray: false, arguments: args, noNullArrayValues: false }) | ||
const args = [{ name: 'input', noNull: true, isArray: false, type: 'UserInput', noNullArrayValues: false, isDeprecated: false }] | ||
expect(schema.RootMutation.fields).to.have.deep.include({ name: 'createUser', type: 'User', noNull: false, isArray: false, arguments: args, noNullArrayValues: false, isDeprecated: false }) | ||
}) | ||
@@ -62,3 +63,3 @@ }) | ||
expect(schema.User).to.exist | ||
expect(schema.User.type).to.be.eq('ObjectType') | ||
expect(schema.User.type).to.be.eq(Kind.OBJECT_TYPE_DEFINITION) | ||
expect(schema.User.description).to.be.eq(undefined) | ||
@@ -70,7 +71,7 @@ expect(schema.User.fields.length).to.be.gt(0) | ||
it('Schema should have the properties with the null type and array type', () => { | ||
expect(schema.User.fields).to.have.deep.include({ name: 'id', type: 'ID', noNull: true, isArray: true, arguments: [], noNullArrayValues: true }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'email', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'username', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'fullName', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'lastNames', type: 'String', noNull: true, isArray: true, arguments: [], noNullArrayValues: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'id', type: 'ID', noNull: true, isArray: true, arguments: [], noNullArrayValues: true, isDeprecated: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'email', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'username', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'fullName', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.User.fields).to.have.deep.include({ name: 'lastNames', type: 'String', noNull: true, isArray: true, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
}) | ||
@@ -82,3 +83,3 @@ }) | ||
expect(schema.UserInput).to.exist | ||
expect(schema.UserInput.type).to.be.eq('InputType') | ||
expect(schema.UserInput.type).to.be.eq(Kind.INPUT_OBJECT_TYPE_DEFINITION) | ||
expect(schema.UserInput.description).to.be.eq(undefined) | ||
@@ -90,8 +91,8 @@ expect(schema.UserInput.fields.length).to.be.gt(0) | ||
it('Schema should have the properties with the null type and array type', () => { | ||
expect(schema.UserInput.fields).to.have.deep.include({ name: 'email', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.UserInput.fields).to.have.deep.include({ name: 'username', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.UserInput.fields).to.have.deep.include({ name: 'fullName', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.UserInput.fields).to.have.deep.include({ name: 'password', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false }) | ||
expect(schema.UserInput.fields).to.have.deep.include({ name: 'email', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.UserInput.fields).to.have.deep.include({ name: 'username', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.UserInput.fields).to.have.deep.include({ name: 'fullName', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
expect(schema.UserInput.fields).to.have.deep.include({ name: 'password', type: 'String', noNull: true, isArray: false, arguments: [], noNullArrayValues: false, isDeprecated: false }) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
109431
58.06%23
9.52%1709
252.37%117
2.63%3
50%1
Infinity%