chore: Refactor graphQL queries structure
This commit is contained in:
@@ -1,30 +1,8 @@
|
|||||||
import { GraphQLObjectType, GraphQLSchema, GraphQLString, GraphQLList, GraphQLNonNull } from 'graphql';
|
import { GraphQLSchema } from 'graphql';
|
||||||
import getApps from './queries/get-apps';
|
import rootQuery from './root-query';
|
||||||
import getApp from './queries/get-app';
|
|
||||||
import appType from './types/app';
|
|
||||||
|
|
||||||
const queryType = new GraphQLObjectType({
|
const graphQLSchema = new GraphQLSchema({
|
||||||
name: 'Query',
|
query: rootQuery,
|
||||||
fields: {
|
|
||||||
getApps: {
|
|
||||||
type: GraphQLList(appType),
|
|
||||||
args: {
|
|
||||||
name: { type: GraphQLString }
|
|
||||||
},
|
|
||||||
resolve: (_, { name }) => getApps(name)
|
|
||||||
},
|
|
||||||
getApp: {
|
|
||||||
type: appType,
|
|
||||||
args: {
|
|
||||||
name: { type: GraphQLNonNull(GraphQLString) },
|
|
||||||
},
|
|
||||||
resolve: (_, { name }) => getApp(name)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var graphQLSchema = new GraphQLSchema({
|
|
||||||
query: queryType,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default graphQLSchema;
|
export default graphQLSchema;
|
||||||
|
@@ -1,11 +1,27 @@
|
|||||||
|
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||||
import App from '../../models/app';
|
import App from '../../models/app';
|
||||||
|
import appType from '../types/app';
|
||||||
|
|
||||||
const getApp = (name: string) => {
|
type Params = {
|
||||||
if(!name) {
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const getAppResolver = (params: Params) => {
|
||||||
|
if(!params.name) {
|
||||||
throw new Error('No name provided.')
|
throw new Error('No name provided.')
|
||||||
}
|
}
|
||||||
|
|
||||||
return App.findOneByName(name)
|
return App.findOneByName(params.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getApp = {
|
||||||
|
type: appType,
|
||||||
|
args: {
|
||||||
|
name: { type: GraphQLNonNull(GraphQLString) },
|
||||||
|
},
|
||||||
|
resolve: (_: any, params: Params) => getAppResolver(params)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default getApp;
|
export default getApp;
|
||||||
|
@@ -1,7 +1,21 @@
|
|||||||
|
import { GraphQLString, GraphQLList } from 'graphql';
|
||||||
|
import appType from '../types/app';
|
||||||
import App from '../../models/app';
|
import App from '../../models/app';
|
||||||
|
|
||||||
const getApps = (name: string) => {
|
type Params = {
|
||||||
return App.findAll(name)
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const getAppsResolver = (params: Params) => {
|
||||||
|
return App.findAll(params.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
const getApps = {
|
||||||
|
type: GraphQLList(appType),
|
||||||
|
args: {
|
||||||
|
name: { type: GraphQLString }
|
||||||
|
},
|
||||||
|
resolve: (_: any, params: Params) => getAppsResolver(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default getApps;
|
export default getApps;
|
||||||
|
13
packages/backend/src/graphql/root-query.ts
Normal file
13
packages/backend/src/graphql/root-query.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { GraphQLObjectType } from 'graphql';
|
||||||
|
import getApps from './queries/get-apps';
|
||||||
|
import getApp from './queries/get-app';
|
||||||
|
|
||||||
|
const rootQuery = new GraphQLObjectType({
|
||||||
|
name: 'Query',
|
||||||
|
fields: {
|
||||||
|
getApps: getApps,
|
||||||
|
getApp: getApp
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default rootQuery;
|
@@ -1,7 +0,0 @@
|
|||||||
import getApps from './queries/get-apps';
|
|
||||||
|
|
||||||
const rootResolver = {
|
|
||||||
getApps: getApps
|
|
||||||
};
|
|
||||||
|
|
||||||
export default rootResolver;
|
|
@@ -1,5 +1,4 @@
|
|||||||
import { graphqlHTTP } from 'express-graphql';
|
import { graphqlHTTP } from 'express-graphql';
|
||||||
import rootResolver from '../graphql/root-resolver'
|
|
||||||
import graphQLSchema from '../graphql/graphql-schema'
|
import graphQLSchema from '../graphql/graphql-schema'
|
||||||
|
|
||||||
const graphQLInstance = graphqlHTTP({
|
const graphQLInstance = graphqlHTTP({
|
||||||
|
Reference in New Issue
Block a user