Ostatnio sobię grzebie w graphQL na expressie. Trafiłem na zagwozdkę:
Chce by Typ schematu był jak najczystszy, bez pól funkcyjnych, by nie urosło to do 200 lini. Przy samej inicjalizacji serwera chciałem dekorować tymi polami funkcyjnymi, ale po poszukiwaniach w bilioteczce, nie ma metody która zwraca konfigurację typu, wiem, że można ograć to za pomocą mixinów ale nie chcę. Ktoś kiedyś próbował z tym walczyć? Albo JA źle myślę.
No to czas na kod;
PlayerTypes.js
let {GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
GraphQLBoolean,
GraphQLInt
} = require('graphql');
const AuthorType = new GraphQLObjectType({
name: "Player",
description: "This represent an player",
fields: () => ({
id: {type: new GraphQLNonNull(GraphQLString)},
nick: {type: new GraphQLNonNull(GraphQLString)},
team: {type: new GraphQLNonNull(GraphQLString)},
warned: {type: new GraphQLNonNull(GraphQLBoolean)},
banned: {type: new GraphQLNonNull(GraphQLBoolean)},
howMuchWarn: {type: GraphQLInt},
welcome: {
type: GraphQLString,
resolve: function() {
return 'Api cup is working :)'
}
}
})
});
module.exports = AuthorType;
Schema
let { GraphQLSchema } = require('graphql');
let PlayersTypes = require('../types/PlayerTypes');
const PlayerSchema = new GraphQLSchema({
query: PlayersTypes
});
module.exports = PlayerSchema;