mirror of
https://github.com/aleleba/create-node-ts-graphql-server.git
synced 2025-06-20 04:48:24 -06:00
PR-757991: adding typegraphql.
This commit is contained in:
@ -1,23 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
import { getTest, addText } from '@controllerGraphQL';
|
||||
|
||||
// A map of functions which return data for the schema.
|
||||
const resolvers = {
|
||||
Query: {
|
||||
// eslint-disable-next-line
|
||||
test: (rootValue: any, args: any, context: any) => ({}),
|
||||
},
|
||||
Mutation: {
|
||||
// eslint-disable-next-line
|
||||
testMutation: (rootValue: any, args: any, context: any) => ({}),
|
||||
},
|
||||
Test: {
|
||||
test: (rootValue: any, args: any, context: any) => getTest({rootValue, args, context})
|
||||
},
|
||||
TestMutation: {
|
||||
testMutation: (rootValue: any, args: any, context: any) => addText({rootValue, args, context})
|
||||
}
|
||||
};
|
||||
|
||||
export default resolvers;
|
18
src/GraphQL/resolvers/test.resolver.ts
Normal file
18
src/GraphQL/resolvers/test.resolver.ts
Normal file
@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
import { Query, Resolver, Mutation, Arg } from 'type-graphql';
|
||||
import { Test, TestMutation } from '@GraphQL/schema/test.schema';
|
||||
|
||||
@Resolver(() => Test)
|
||||
export class TestResolver {
|
||||
@Query(() => Test)
|
||||
async test() {
|
||||
return {};
|
||||
}
|
||||
|
||||
@Mutation(() => TestMutation)
|
||||
async testMutation() {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
module.exports = `
|
||||
|
||||
"""Test Query"""
|
||||
type Test {
|
||||
test: String
|
||||
}
|
||||
|
||||
"""Esta es la Data de LogIn, Si los datos no son correctos devuelve el usuario Null y la conexion en False"""
|
||||
type TestMutation {
|
||||
testMutation(text: String): String
|
||||
}
|
||||
|
||||
`
|
@ -1,17 +1,10 @@
|
||||
import { makeExecutableSchema } from '@graphql-tools/schema';
|
||||
import resolvers from'@GraphQL/resolvers';
|
||||
import Test from '@GraphQL/schema/Test.gql';
|
||||
'use strict'
|
||||
|
||||
// The GraphQL schema
|
||||
const rootTypes = `
|
||||
type Query {
|
||||
test: Test
|
||||
}
|
||||
type Mutation {
|
||||
testMutation: TestMutation
|
||||
}
|
||||
`;
|
||||
import { buildSchemaSync } from "type-graphql"
|
||||
import { TestResolver } from "@GraphQL/resolvers/test.resolver";
|
||||
|
||||
const typeDefs = [ rootTypes, Test ];
|
||||
|
||||
export default makeExecutableSchema({typeDefs, resolvers});
|
||||
const schema = buildSchemaSync({
|
||||
resolvers: [TestResolver],
|
||||
emitSchemaFile: true,
|
||||
})
|
||||
export default schema
|
36
src/GraphQL/schema/test.schema.ts
Normal file
36
src/GraphQL/schema/test.schema.ts
Normal file
@ -0,0 +1,36 @@
|
||||
'use strict';
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@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
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
import express from 'express'; //express
|
||||
import { graphqlHTTP } from 'express-graphql';
|
||||
import { config } from '@config';
|
||||
import schema from '@GraphQL/schema';
|
||||
import schema from '@src/GraphQL/schema';
|
||||
|
||||
|
||||
const server = express.Router();//Router de Express
|
||||
|
||||
@ -17,8 +19,6 @@ server.use(
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
|
||||
// DO NOT DO app.listen() unless we're testing this directly
|
||||
if (require.main === module) {
|
||||
const app = express();
|
||||
|
@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
import { getTestModel, addTextModel } from '@models';
|
||||
import { TextMutation } from '@src/GraphQL/schema/test.schema';
|
||||
|
||||
// eslint-disable-next-line
|
||||
export const getTest = async ({}) => {
|
||||
@ -8,11 +9,6 @@ export const getTest = async ({}) => {
|
||||
};
|
||||
|
||||
// eslint-disable-next-line
|
||||
export const addText = async ({args}: {
|
||||
rootValue: any
|
||||
args: { text: string }
|
||||
context: any
|
||||
}) => {
|
||||
const text = args.text;
|
||||
export const addText = async ({ text }: TextMutation) => {
|
||||
return addTextModel({ text });
|
||||
};
|
@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
import "reflect-metadata";
|
||||
import ws from 'ws'; // yarn add ws
|
||||
import express from 'express'; //express
|
||||
import cors from 'cors';
|
||||
|
@ -1,9 +1,11 @@
|
||||
'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 }: {text: string}) => {
|
||||
export const addTextModel = async ({ text }: TextMutation) => {
|
||||
return `Simulate to insert some text: ${text} from a model`;
|
||||
};
|
@ -2,17 +2,21 @@ import server from '@src';
|
||||
import supertest from 'supertest';
|
||||
describe('global server tests', () => {
|
||||
let request: supertest.SuperTest<supertest.Test>;
|
||||
beforeEach(() => {
|
||||
request = supertest(server);
|
||||
});
|
||||
beforeEach( async () => {
|
||||
request = await supertest(server);
|
||||
});
|
||||
|
||||
it('should return Test data from test Query', async () => {
|
||||
const bodyResponse = {
|
||||
data: {
|
||||
test: { test: 'This is the text response for Test Query from a model' }
|
||||
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%20test%0A%20%20%7D%0A%7D%0A')
|
||||
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);
|
||||
@ -21,14 +25,21 @@ describe('global server tests', () => {
|
||||
it('should return Test data from test Mutation', async () => {
|
||||
const bodyResponse = {
|
||||
data: {
|
||||
testMutation: {
|
||||
testMutation: 'Simulate to insert some text: test text from a model'
|
||||
testMutation: {
|
||||
textMutation: {
|
||||
text: "Simulate to insert some text: testing text from a model"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const response = await request.post('/graphql')
|
||||
.send({"query":"mutation{\n testMutation{\n testMutation(text: \"test text\")\n }\n}\n","variables":null})
|
||||
.send({'query': `mutation{
|
||||
testMutation{
|
||||
textMutation(text: "testing text"){
|
||||
text
|
||||
}
|
||||
}
|
||||
}`})
|
||||
.set('Accept', 'application/json')
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual(bodyResponse);
|
||||
|
Reference in New Issue
Block a user