Skip to content

Commit

Permalink
new version with newer graphql support
Browse files Browse the repository at this point in the history
  • Loading branch information
aexol committed Apr 5, 2024
1 parent f6df5a6 commit 63ccf8a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 32 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-js-tree",
"version": "1.0.9",
"version": "2.0.0",
"private": false,
"license": "MIT",
"description": "GraphQL Parser providing simplier structure",
Expand Down Expand Up @@ -39,6 +39,6 @@
"typescript-transform-paths": "^3.4.6"
},
"dependencies": {
"graphql": "15.4.0"
"graphql": "^16.8.1"
}
}
3 changes: 2 additions & 1 deletion src/GqlParser/GqlParserTreeToGql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getValueAsGqlStringNode } from '@/GqlParser/valueNode';
import { Instances, TypeSystemDefinition } from '@/Models';
import { GqlParserTree, VariableDefinitionWithoutLoc } from '@/Models/GqlParserTree';
import { compileType } from '@/shared';
import { Kind } from 'graphql';
export const parseGqlTree = (mainTree: GqlParserTree) => {
const generateName = (tree: GqlParserTree): string => {
if (tree.operation) {
Expand Down Expand Up @@ -65,7 +66,7 @@ export const enrichFieldNodeWithVariables = (
name: a.name,
node: a,
value: {
kind: 'Variable',
kind: Kind.VARIABLE,
value: VarName,
},
};
Expand Down
4 changes: 2 additions & 2 deletions src/GqlParser/valueNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ValueNodeWithoutLoc } from '@/Models/GqlParserTree';
import { ValueNode } from 'graphql';
import { Kind, ValueNode } from 'graphql';

export const getValueAsGqlStringNode = (v: ValueNodeWithoutLoc): string => {
if (v.kind === 'ListValue') {
Expand Down Expand Up @@ -41,7 +41,7 @@ export const getValueWithoutLoc = (v: ValueNode): ValueNodeWithoutLoc => {
return {
kind: v.kind,
fields: v.fields.map((f) => ({
kind: 'ObjectField',
kind: Kind.OBJECT_FIELD,
name: f.name.value,
value: getValueWithoutLoc(f.value),
})),
Expand Down
13 changes: 7 additions & 6 deletions src/__tests__/GqlParser/GqlParserTreeToGql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { OperationType, TypeDefinition } from '@/Models';
import { GqlParserTree } from '@/Models/GqlParserTree';
import { createPlainField, createPlainInputValue, createRootField, createTypeNameField } from '@/shared';
import { expectTrimmedEqual } from '@/__tests__/TestUtils';
import { Kind } from 'graphql';

const mockSchema = `
type Query {
Expand Down Expand Up @@ -113,29 +114,29 @@ describe('Test generation of gql strings from the GqlParserTree', () => {
name: 'maxValue',
node: queryNode.args[0].args[0],
value: {
kind: 'IntValue',
kind: Kind.INT,
value: '100',
},
},
{
name: 'score',
node: queryNode.args[0].args[1],
value: {
kind: 'ObjectValue',
kind: Kind.OBJECT,
fields: [
{
kind: 'ObjectField',
kind: Kind.OBJECT_FIELD,
name: 'value',
value: {
kind: 'FloatValue',
kind: Kind.FLOAT,
value: '1.0',
},
},
{
kind: 'ObjectField',
kind: Kind.OBJECT_FIELD,
name: 'name',
value: {
kind: 'StringValue',
kind: Kind.STRING,
value: 'Hello',
},
},
Expand Down
17 changes: 9 additions & 8 deletions src/__tests__/GqlParser/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { parseGql } from '@/GqlParser';
import { OperationType, TypeDefinition } from '@/Models';
import { GqlParserTree } from '@/Models/GqlParserTree';
import { createPlainField, createPlainInputValue, createRootField, createTypeNameField } from '@/shared';
import { Kind } from 'graphql';

const mockSchema = `
type Query {
Expand Down Expand Up @@ -107,29 +108,29 @@ describe('Test generation of GqlParserTrees from gql', () => {
name: 'maxValue',
node: queryNode.args[0].args[0],
value: {
kind: 'IntValue',
kind: Kind.INT,
value: '100',
},
},
{
name: 'score',
node: queryNode.args[0].args[1],
value: {
kind: 'ObjectValue',
kind: Kind.OBJECT,
fields: [
{
kind: 'ObjectField',
kind: Kind.OBJECT_FIELD,
name: 'value',
value: {
kind: 'FloatValue',
kind: Kind.FLOAT,
value: '1.0',
},
},
{
kind: 'ObjectField',
kind: Kind.OBJECT_FIELD,
name: 'name',
value: {
kind: 'StringValue',
kind: Kind.STRING,
value: 'Hello',
},
},
Expand Down Expand Up @@ -173,15 +174,15 @@ describe('Test generation of GqlParserTrees from gql', () => {
name: 'maxValue',
node: queryNode.args[0].args[0],
value: {
kind: 'Variable',
kind: Kind.VARIABLE,
value: 'maxValue',
},
},
{
name: 'score',
node: queryNode.args[0].args[1],
value: {
kind: 'Variable',
kind: Kind.VARIABLE,
value: 'score',
},
},
Expand Down
27 changes: 14 additions & 13 deletions src/__tests__/GqlParser/valueNode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
import { getValueAsGqlStringNode } from '@/GqlParser/valueNode';
import { expectTrimmedEqual } from '@/__tests__/TestUtils';
import { Kind } from 'graphql';

describe('Test generation of value strings from the GqlParserTree', () => {
it('Generates Correct Variable Value', () => {
const strValue = getValueAsGqlStringNode({ kind: 'Variable', value: 'Hello' });
const strValue = getValueAsGqlStringNode({ kind: Kind.VARIABLE, value: 'Hello' });
expect(strValue).toEqual(`$Hello`);
});
it('Generates Correct String Value', () => {
const strValue = getValueAsGqlStringNode({ kind: 'StringValue', value: 'Hello' });
const strValue = getValueAsGqlStringNode({ kind: Kind.STRING, value: 'Hello' });
expect(strValue).toEqual(`"Hello"`);
});
it('Generates Correct Int Value', () => {
const strValue = getValueAsGqlStringNode({ kind: 'IntValue', value: '100' });
const strValue = getValueAsGqlStringNode({ kind: Kind.INT, value: '100' });
expect(strValue).toEqual(`100`);
});
it('Generates Correct Float Value', () => {
const strValue = getValueAsGqlStringNode({ kind: 'FloatValue', value: '100.0' });
const strValue = getValueAsGqlStringNode({ kind: Kind.FLOAT, value: '100.0' });
expect(strValue).toEqual(`100.0`);
});
it('Generates Correct Boolean Value', () => {
const strValue = getValueAsGqlStringNode({ kind: 'BooleanValue', value: false });
const strValue = getValueAsGqlStringNode({ kind: Kind.BOOLEAN, value: false });
expect(strValue).toEqual(`false`);
});
it('Generates Correct Enum Value', () => {
const strValue = getValueAsGqlStringNode({ kind: 'EnumValue', value: 'HELLO' });
const strValue = getValueAsGqlStringNode({ kind: Kind.ENUM, value: 'HELLO' });
expect(strValue).toEqual(`HELLO`);
});
it('Generates Correct Null Value', () => {
const strValue = getValueAsGqlStringNode({ kind: 'NullValue', value: null });
const strValue = getValueAsGqlStringNode({ kind: Kind.NULL, value: null });
expect(strValue).toEqual(`null`);
});
it('Generates Correct List Value', () => {
const strValue = getValueAsGqlStringNode({
kind: 'ListValue',
kind: Kind.LIST,
values: [
{ kind: 'StringValue', value: 'Hello' },
{ kind: 'StringValue', value: 'World' },
{ kind: Kind.STRING, value: 'Hello' },
{ kind: Kind.STRING, value: 'World' },
],
});
expectTrimmedEqual(strValue, `["Hello", "World"]`);
});
it('Generates Correct Object Value', () => {
const strValue = getValueAsGqlStringNode({
kind: 'ObjectValue',
kind: Kind.OBJECT,
fields: [
{ kind: 'ObjectField', name: 'word', value: { kind: 'StringValue', value: 'Hello' } },
{ kind: 'ObjectField', name: 'word2', value: { kind: 'StringValue', value: 'Hello' } },
{ kind: Kind.OBJECT_FIELD, name: 'word', value: { kind: Kind.STRING, value: 'Hello' } },
{ kind: Kind.OBJECT_FIELD, name: 'word2', value: { kind: Kind.STRING, value: 'Hello' } },
],
});
expectTrimmedEqual(
Expand Down

0 comments on commit 63ccf8a

Please sign in to comment.