Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
{ id: 1, firstName: "Tom", lastName: "Coleman" },
{ id: 2, firstName: "Sashko", lastName: "Stubailo" },
{ id: 3, firstName: "Mikhail", lastName: "Novikov" },
]
const posts = [
{ id: 1, authorId: 1, title: "Introduction to GraphQL", votes: 2 },
{ id: 2, authorId: 2, title: "Welcome to Apollo", votes: 3 },
{ id: 3, authorId: 2, title: "Advanced GraphQL", votes: 1 },
{ id: 4, authorId: 3, title: "Launchpad is Cool", votes: 7 },
]
// -----------------------------------------------------------------------------
// #2. Creating Types
const AuthorTC = TypeComposer.create({
name: "Author",
fields: {
id: "Int!",
firstName: "String",
lastName: "String",
},
})
const PostTC = TypeComposer.create({
name: "Post",
fields: {
id: "Int!",
title: "String",
votes: "Int",
authorId: "Int",
},
{ id: 4, authorId: 3, title: "Launchpad is Cool", votes: 7 },
]
// -----------------------------------------------------------------------------
// #2. Creating Types
const AuthorTC = TypeComposer.create({
name: "Author",
fields: {
id: "Int!",
firstName: "String",
lastName: "String",
},
})
const PostTC = TypeComposer.create({
name: "Post",
fields: {
id: "Int!",
title: "String",
votes: "Int",
authorId: "Int",
},
})
// -----------------------------------------------------------------------------
// #3. Create relations between Types
PostTC.addFields({
author: {
// you may provide type name as string 'Author',
// but for better developer experience use Type instance `AuthorTC`
const { PlantTC, GardenTC, MuseumTC, HerbariumTC, PlantSearchResultItemType } = modelsTC
const CategoryEnum = new GraphQLEnumType({
name: 'CategoryEnum',
values: require('../../category')
})
const AutoCompleteResultItem = new GraphQLObjectType({
name: 'ScientificNameItem',
fields: {
scientificName: { type: GraphQLString },
familyName: { type: GraphQLString },
name: { type: GraphQLString },
_id: { type: GraphQLString }
}
})
const AutoCompleteResultItemTC = TypeComposer.create(AutoCompleteResultItem)
AutoCompleteResultItemTC.extendField('scientificName', {
resolve: source => (scientificSplit(source.scientificName))
})
const PlantSearchResolver = new Resolver({
name: 'search',
type: new GraphQLObjectType({
name: 'PlantSearchResult',
fields: {
result: { type: new GraphQLList(PlantSearchResultItemType) },
count: { type: 'Int' }
}
}),
args: {
text: { type: '[String]', defaultValue: [] },
categories: { type: new GraphQLList(CategoryEnum), defaultValue: ['garden', 'herbarium', 'museum'] },
exports.convertToSourceTC = async (opts = {}) => {
const tc = TypeComposer.create({
name: `${opts.prefix || ""}${opts.graphqlTypeName}${opts.postfix || ""}`,
})
const fieldsMap = await retrieveTableFields(opts.mysqlClient, opts.mysqlTable)
const fields = {}
Object.keys(fieldsMap).forEach(field => {
const fieldName = fieldsMap[field].Field
fields[fieldName] = mysqlTypeToGraphQLType(fieldsMap[field].Type)
})
console.log(tc)
tc.addFields(fields)
return tc
module.exports = () => {
const { TypeComposer } = require('graphql-compose')
const { GraphQLList } = require('graphql')
const ErrorTC = TypeComposer.create(`
type Error {
code: Int,
message: String!,
}
`)
const ErrorResolver = require('../resolvers/ErrorResolver')
ErrorTC.addResolver({
name: 'error',
kind: 'query',
type: new GraphQLList(ErrorTC.getType()),
resolve: ErrorResolver.resolver
})
return { ErrorTC }
const _ = require('lodash')
const mongoose = require('mongoose')
const { TypeComposer } = require('graphql-compose')
const { GraphQLList } = require('graphql')
const FileType = TypeComposer.create('File')
FileType.addFields({
url: { type: 'String' }
})
exports.AddTypeToImageField = (TC) => {
TC.removeField('images')
TC.addFields({
images: {
type: new GraphQLList(FileType.getType()),
resolve: source => source.images
},
thumbnailImage: {
projection: { images: 1 },
type: 'String',
resolve: (source) => {
if (source.images.length === 0) {
return '/static/images/placeholder150x150.png'