Merge pull request #28 from aleleba/PR-442185

PR-442185: fixing the type graphql schema.
This commit is contained in:
Alejandro Lembke Barrientos 2023-03-13 18:17:52 -06:00 committed by GitHub
commit ad00e689c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 86 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@aleleba/create-node-ts-graphql-server", "name": "@aleleba/create-node-ts-graphql-server",
"version": "1.4.0", "version": "1.4.1",
"description": "Node with Typescript and GraphQL Server", "description": "Node with Typescript and GraphQL Server",
"bin": "./bin/cli.js", "bin": "./bin/cli.js",
"main": "index.js", "main": "index.js",

View File

@ -12,17 +12,9 @@ type Query {
} }
type Test { type Test {
text: Text! text: String!
} }
type TestMutation { type TestMutation {
textMutation(text: String!): TextMutation! text(text: String!): String!
}
type Text {
text: String!
}
type TextMutation {
text: String!
} }

View File

@ -1,18 +1,19 @@
/* eslint-disable no-mixed-spaces-and-tabs */
'use strict'; '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'; import { Test, TestMutation } from '@GraphQL/schema/test.schema';
@Resolver(() => Test) @Resolver(() => Test)
export class TestResolver { export class TestResolver {
@Query(() => Test) @Query(() => Test)
async test() { async test() {
return {}; return Test;
} }
@Mutation(() => TestMutation) @Mutation(() => TestMutation)
async testMutation() { async testMutation() {
return {}; return TestMutation;
} }
} }

View File

@ -1,36 +1,21 @@
/* eslint-disable no-mixed-spaces-and-tabs */
'use strict'; 'use strict';
import { Field, ObjectType, Arg } from "type-graphql"; import { Field, ObjectType, Arg } from 'type-graphql';
import { getTest, addText } from '@controllerGraphQL'; import { getTest, addText } from '@controllerGraphQL';
@ObjectType() @ObjectType()
export class Test { export class Test {
@Field(() => Text) @Field(() => String)
async text(){ async text(){
return { return await getTest({});
text: await getTest({}) }
}
}
}
@ObjectType()
export class Text {
@Field()
text?: string
} }
@ObjectType() @ObjectType()
export class TestMutation { export class TestMutation {
@Field(type => TextMutation) @Field(() => String)
async textMutation(@Arg('text') text?: string){ async text(@Arg('text') text?: string){
return { return await addText({text});
text: await addText({text}) }
}
}
}
@ObjectType()
export class TextMutation {
@Field()
text?: string
} }

View File

@ -1,7 +1,6 @@
'use strict'; 'use strict';
import { getTestModel, addTextModel } from '@models'; import { getTestModel, addTextModel } from '@models';
import { TextMutation } from '@src/GraphQL/schema/test.schema';
// eslint-disable-next-line // eslint-disable-next-line
export const getTest = async ({}) => { export const getTest = async ({}) => {
@ -9,6 +8,6 @@ export const getTest = async ({}) => {
}; };
// eslint-disable-next-line // eslint-disable-next-line
export const addText = async ({ text }: TextMutation) => { export const addText = async ({ text }: {text?: string}) => {
return addTextModel({ text }); return addTextModel({ text });
}; };

View File

@ -1,11 +1,9 @@
'use strict'; 'use strict';
import { TextMutation } from "@GraphQL/schema/test.schema";
export const getTestModel = async () => { export const getTestModel = async () => {
return 'This is the text response for Test Query from a model'; 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`; return `Simulate to insert some text: ${text} from a model`;
}; };

View File

@ -1,47 +1,41 @@
import server from '@src'; import server from '@src';
import supertest from 'supertest'; import supertest from 'supertest';
describe('global server tests', () => { describe('global server tests', () => {
let request: supertest.SuperTest<supertest.Test>; let request: supertest.SuperTest<supertest.Test>;
beforeEach( async () => { beforeEach( async () => {
request = await supertest(server); request = await supertest(server);
}); });
it('should return Test data from test Query', async () => { it('should return Test data from test Query', async () => {
const bodyResponse = { const bodyResponse = {
data: { data: {
test: { test: {
text: { text: 'This is the text response for Test Query from a model'
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');
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') expect(response.status).toEqual(200);
.set('Accept', 'application/json') expect(response.body).toEqual(bodyResponse);
expect(response.status).toEqual(200); });
expect(response.body).toEqual(bodyResponse);
});
it('should return Test data from test Mutation', async () => { it('should return Test data from test Mutation', async () => {
const bodyResponse = { const bodyResponse = {
data: { data: {
testMutation: { testMutation: {
textMutation: { text: 'Simulate to insert some text: testing text from a model'
text: "Simulate to insert some text: testing text from a model" }
} }
} };
} const response = await request.post('/graphql')
} .send({'query': `mutation{
const response = await request.post('/graphql') testMutation{
.send({'query': `mutation{ text(text: "testing text")
testMutation{ }
textMutation(text: "testing text"){
text
}
}
}`}) }`})
.set('Accept', 'application/json') .set('Accept', 'application/json');
expect(response.status).toEqual(200); expect(response.status).toEqual(200);
expect(response.body).toEqual(bodyResponse); expect(response.body).toEqual(bodyResponse);
}); });
}); });