From 00a60519a2854733a85713a48864456249c56629 Mon Sep 17 00:00:00 2001 From: Alejandro Lembke Barrientos Date: Tue, 14 Mar 2023 00:15:33 +0000 Subject: [PATCH] PR-442185: fixing the type graphql schema. --- package.json | 2 +- schema.gql | 12 +--- src/GraphQL/resolvers/test.resolver.ts | 11 ++-- src/GraphQL/schema/test.schema.ts | 35 +++------- src/controllers/controllerGraphQL/index.ts | 3 +- src/models/index.ts | 4 +- src/tests/server/index.test.ts | 74 ++++++++++------------ 7 files changed, 55 insertions(+), 86 deletions(-) diff --git a/package.json b/package.json index 07adaf2..e7ba301 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@aleleba/create-node-ts-graphql-server", - "version": "1.4.0", + "version": "1.4.1", "description": "Node with Typescript and GraphQL Server", "bin": "./bin/cli.js", "main": "index.js", diff --git a/schema.gql b/schema.gql index 6820687..9bbac58 100644 --- a/schema.gql +++ b/schema.gql @@ -12,17 +12,9 @@ type Query { } type Test { - text: Text! + text: String! } type TestMutation { - textMutation(text: String!): TextMutation! -} - -type Text { - text: String! -} - -type TextMutation { - text: String! + text(text: String!): String! } \ No newline at end of file diff --git a/src/GraphQL/resolvers/test.resolver.ts b/src/GraphQL/resolvers/test.resolver.ts index eb97de1..9f279d7 100644 --- a/src/GraphQL/resolvers/test.resolver.ts +++ b/src/GraphQL/resolvers/test.resolver.ts @@ -1,18 +1,19 @@ +/* eslint-disable no-mixed-spaces-and-tabs */ 'use strict'; -import { Query, Resolver, Mutation, Arg } from 'type-graphql'; +import { Query, Resolver, Mutation } from 'type-graphql'; import { Test, TestMutation } from '@GraphQL/schema/test.schema'; @Resolver(() => Test) export class TestResolver { @Query(() => Test) - async test() { - return {}; - } + async test() { + return Test; + } @Mutation(() => TestMutation) async testMutation() { - return {}; + return TestMutation; } } diff --git a/src/GraphQL/schema/test.schema.ts b/src/GraphQL/schema/test.schema.ts index 4863e44..e5025a0 100644 --- a/src/GraphQL/schema/test.schema.ts +++ b/src/GraphQL/schema/test.schema.ts @@ -1,36 +1,21 @@ +/* eslint-disable no-mixed-spaces-and-tabs */ 'use strict'; -import { Field, ObjectType, Arg } from "type-graphql"; +import { Field, ObjectType, Arg } from 'type-graphql'; import { getTest, addText } from '@controllerGraphQL'; @ObjectType() export class Test { - @Field(() => Text) - async text(){ - return { - text: await getTest({}) - } - } -} - -@ObjectType() -export class Text { - @Field() - text?: string + @Field(() => String) + async text(){ + return await getTest({}); + } } @ObjectType() export class TestMutation { - @Field(type => TextMutation) - async textMutation(@Arg('text') text?: string){ - return { - text: await addText({text}) - } - } -} - -@ObjectType() -export class TextMutation { - @Field() - text?: string + @Field(() => String) + async text(@Arg('text') text?: string){ + return await addText({text}); + } } diff --git a/src/controllers/controllerGraphQL/index.ts b/src/controllers/controllerGraphQL/index.ts index 56d717f..6c42e87 100644 --- a/src/controllers/controllerGraphQL/index.ts +++ b/src/controllers/controllerGraphQL/index.ts @@ -1,7 +1,6 @@ 'use strict'; import { getTestModel, addTextModel } from '@models'; -import { TextMutation } from '@src/GraphQL/schema/test.schema'; // eslint-disable-next-line export const getTest = async ({}) => { @@ -9,6 +8,6 @@ export const getTest = async ({}) => { }; // eslint-disable-next-line -export const addText = async ({ text }: TextMutation) => { +export const addText = async ({ text }: {text?: string}) => { return addTextModel({ text }); }; \ No newline at end of file diff --git a/src/models/index.ts b/src/models/index.ts index 15ada4e..167a389 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,11 +1,9 @@ 'use strict'; -import { TextMutation } from "@GraphQL/schema/test.schema"; - export const getTestModel = async () => { return 'This is the text response for Test Query from a model'; }; -export const addTextModel = async ({ text }: TextMutation) => { +export const addTextModel = async ({ text }: {text?: string}) => { return `Simulate to insert some text: ${text} from a model`; }; \ No newline at end of file diff --git a/src/tests/server/index.test.ts b/src/tests/server/index.test.ts index 65261c4..1c2721c 100644 --- a/src/tests/server/index.test.ts +++ b/src/tests/server/index.test.ts @@ -1,47 +1,41 @@ import server from '@src'; import supertest from 'supertest'; describe('global server tests', () => { - let request: supertest.SuperTest; - beforeEach( async () => { - request = await supertest(server); - }); + let request: supertest.SuperTest; + beforeEach( async () => { + request = await supertest(server); + }); - it('should return Test data from test Query', async () => { - const bodyResponse = { - data: { - test: { - text: { - text: "This is the text response for Test Query from a model" - } - } - } - } - const response = await request.get('/graphql?query=%7B%0A%20%20test%7B%0A%20%20%20%20text%7B%0A%20%20%20%20%20%20text%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0A') - .set('Accept', 'application/json') - expect(response.status).toEqual(200); - expect(response.body).toEqual(bodyResponse); - }); + it('should return Test data from test Query', async () => { + const bodyResponse = { + data: { + test: { + text: 'This is the text response for Test Query from a model' + } + } + }; + const response = await request.get('/graphql?query=%7B%0A%20%20test%7B%0A%20%20%20%20text%0A%20%20%7D%0A%7D') + .set('Accept', 'application/json'); + expect(response.status).toEqual(200); + expect(response.body).toEqual(bodyResponse); + }); - it('should return Test data from test Mutation', async () => { - const bodyResponse = { - data: { - testMutation: { - textMutation: { - text: "Simulate to insert some text: testing text from a model" - } - } - } - } - const response = await request.post('/graphql') - .send({'query': `mutation{ - testMutation{ - textMutation(text: "testing text"){ - text - } - } + it('should return Test data from test Mutation', async () => { + const bodyResponse = { + data: { + testMutation: { + text: 'Simulate to insert some text: testing text from a model' + } + } + }; + const response = await request.post('/graphql') + .send({'query': `mutation{ + testMutation{ + text(text: "testing text") + } }`}) - .set('Accept', 'application/json') - expect(response.status).toEqual(200); - expect(response.body).toEqual(bodyResponse); - }); + .set('Accept', 'application/json'); + expect(response.status).toEqual(200); + expect(response.body).toEqual(bodyResponse); + }); }); \ No newline at end of file