Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'graphql-compose' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
// @flow
import { ApolloServer } from 'apollo-server';
import { schemaComposer } from 'graphql-compose';
import { authors, articles } from './data';
const AuthorType = schemaComposer.createObjectTC(`
"Author data"
type Author {
id: Int
name: String
}
`);
const ArticleType = schemaComposer.createObjectTC({
name: 'Article',
description: 'Article data with related Author data',
fields: {
title: 'String!',
text: 'String',
authorId: 'Int!',
author: {
type: () => AuthorType,
/* @flow */
import { schemaComposer } from 'graphql-compose';
export const rootQueryTC = schemaComposer.createObjectTC({
name: 'RootQuery',
fields: {},
});
// @flow
import { ApolloServer } from 'apollo-server';
import { schemaComposer } from 'graphql-compose';
import { authors, articles } from './data';
const AuthorType = schemaComposer.createObjectTC(`
"Author data"
type Author {
id: Int
name: String
}
`);
const ArticleType = schemaComposer.createObjectTC({
name: 'Article',
description: 'Article data with related Author data',
fields: {
title: 'String!',
text: 'String',
authorId: 'Int!',
author: {
type: () => AuthorType,
resolve: source => {
const { authorId } = source;
return authors.find(o => o.id === authorId);
},
},
},
});
/* @flow */
/* eslint-disable arrow-body-style */
import { upperFirst, ObjectTypeComposer, type SchemaComposer } from 'graphql-compose';
// PaginationInfo should be global
const PaginationInfoTC = ObjectTypeComposer.createTemp(`
# Information about pagination.
type PaginationInfo {
# Current page number
currentPage: Int!
# Number of items per page
perPage: Int!
# Total number of pages
pageCount: Int
# Total number of items
itemCount: Int
# When paginating forwards, are there more items?
hasNextPage: Boolean
if (!opts.prefix) {
opts.prefix = ""
}
PREFIX = Object.freeze(opts.prefix)
// TODO optimize schema creation (use a pool instead of a single connection ?)
const mysqlConnection = mysql.createConnection(opts.mysqlConfig)
// Mix-in for Data Access Methods and SQL Autogenerating Methods
mysqlUtilities.upgrade(mysqlConnection)
// Mix-in for Introspection Methods
mysqlUtilities.introspection(mysqlConnection)
// initialize the graphQL schema
const schemaComposer = new SchemaComposer()
const mysqlTablesNames = await _getMysqlTablesNames(mysqlConnection)
return Promise.all(mysqlTablesNames.map(async mysqlTableName => {
// initialize the graphql type built from the mysql table
const gqlTC = schemaComposer.TypeComposer.create({
name: _clearNameForType(mysqlTableName),
})
// add local fields
const fields = await _buildGqlFieldsFromMysqlTable(mysqlConnection, mysqlTableName)
gqlTC.addFields(fields)
// add local resolver
const resolver = _buildResolverForGqlType(opts.mysqlConfig, mysqlTableName, gqlTC)
schemaComposer.Query.addFields({ [resolver.name]: resolver })
/* @flow */
// SINGLE SCHEMA ON SERVER
// import { schemaComposer } from 'graphql-compose';
// MULTI SCHEMA MODE IN ONE SERVER
// import { SchemaComposer } from 'graphql-compose';
// const schemaComposer = new SchemaComposer();
import { SchemaComposer } from 'graphql-compose';
export type TContext = {
ip: string,
};
export const schemaComposer: SchemaComposer = new SchemaComposer();
password: String,
dob: String,
createdAt: Date,
updatedAt: Date,
});
import { composeType, composeField, composeResolve, composeInterface, composeStorage } from 'graphql-compose';
import { description, only, remove, restrict, add } from 'graphql-compose/type';
import { composeTypeFromMongoose } from 'graphql-compose-mongoose';
import { GraphQLString } from 'graphql';
//---------- TYPE MODIFICATORS
composeType('User',
composeTypeFromMongoose(UserMongooseModel),
addResolverParam('model', UserMongooseModel), // internally added by `composeTypeFromMongoose`
composeInterface('Timestable'), // internally call composeStorage.Interfaces.get('Timestable')
description('User model description'),
only(['myName', 'surname']), // get only described fields
remove(['stats', 'password']), // ... or leave others, and remove just listed here
rename({
myName: 'name',
surname: 'lastname',
}),
restrict({
hasAccess: (source, args, context, info) => {
return context.isAdmin;
},
fields: ['name', 'dob'],
}),
changeValue({
name: ({ source, args, context, info }) => `${source.name} modified`,
}),
// example of custom type-middleware
next => typeConfig => {
const gqType = next(typeConfig);
return gqType;
},
);
//---------- INHERITANCE
composeType('SuperUser',
cloneType('User'),
add('isSuperUserType',
{
type: GraphQLBoolean,
resolve: () => true,
}
)
);
//---------- FIELD RESOLVERS
composeType('User',
add(
'friends',
makeInputType(),
remove(['id'])
);
composeType('RootMutation',
add('createUser', composeType('User').queryById)
);
//----------- ROOT CONSTRUCTOR (in progress, not ready)
composeType('RootQuery',
add('user', composeType('User').queryById),
add('userList', composeType('User').queryList),
add('userConnection', composeType('User').queryConnection),
add('superUser', composeType('SuperUser').queryById)
);
//----------- INTERFACES
composeInterface('Timestable',
description('Timestable interface for types, which have fields with creation and modification time'),
add('createdAt', {
type: GraphQLDate,
}),
add('updatedAt', {
type: GraphQLDate,
}),
addTypeResolver( // too bad name, need another
(value, info) => {
composeType('SuperUser',
cloneType('User'),
add('isSuperUserType',
{
type: GraphQLBoolean,
resolve: () => true,
}
)
);
//---------- FIELD RESOLVERS
composeType('User',
add(
'friends',
composeField(
// custom field middleware
next => fieldConfig => {
const gqField = next(fieldConfig);
return gqField;
},
description('List of friends'),
addArg('gender', {}),
composeResolve(
argEval(({ source }) => ({ frendId: source._id })),
resolveList('User'),
// example of custom resolve-middleware
next => resolveParams => {