graphQl - return all in db - graphql

I'm really new to graphQL
I have a simple graphQL schema here
const graphql = require('graphql');
const _ = require('lodash');
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID
} = graphql
const books = [
{ name: "book 1", genre: "book-1", id: "1" },
{ name: "book 2", genre: "book-2", id: "2" },
{ name: "book 3", genre: "book-3", id: "3" }
]
const BookType = new GraphQLObjectType({
name: 'Book',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
genre: { type: GraphQLString }
})
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return _.find(books, { id: args.id })
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery
})
I can return one book using the id
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return _.find(books, { id: args.id })
}
}
}
})
How would I return all the books, I was thinking something like:
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: {},
resolve(parent, args) {
return _.find(books, {})
}
}
}
})

const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: { displayall: { type: GraphQLID } },
resolve(parent, args) {
return _.find(books, { displayall: args.displayall })
}
}
books:{
type: new GraphQLList(BookType),
resolve(parent, args) {
return books
}
}
}
})

Related

Return the wrong response if type of is number in the graphql

I tried to run the basic query in the graphql. But the below query is always return the no data if the typeof id is number. incase typeof id is string it returns the correct respone.
{
movie(id: 1) {
id
name
}
}
respone
{
"data": {
"movie": null
}
}
attached my code below
const graphql = require("graphql");
const _ = require("lodash");
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLInt,
GraphQLList,
} = graphql;
const movies = [
{ name: "book1", genre: "Thriller", id: 1, directorId: 1 },
{ name: "book2", genre: "Horror", id: 2, directorId: 1 },
{ name: "book3", genre: "Historical", id: 3, directorId: 2 },
{ name: "book4", genre: "Thriller", id: 4, directorId: 1 },
{ name: "book5", genre: "Science Fiction", id: 5, directorId: 2 },
{ name: "book6", genre: "Horror", id: 6, directorId: 3 },
{ name: "book7", genre: "Romance", id: 7, directorId: 1 },
];
const directors = [
{ name: "directors1", age: "26", id: 1 },
{ name: "directors2", age: "36", id: 2 },
{ name: "directors3", age: "46", id: 3 },
];
const MovieType = new GraphQLObjectType({
name: "Movie",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
genre: { type: GraphQLString },
director: {
type: DirectorType,
resolve: (parent, args) => {
return _.find(directors, { id: parent.directorId });
},
},
}),
});
const DirectorType = new GraphQLObjectType({
name: "Director",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
age: { type: GraphQLInt },
movies: {
type: MovieType,
resolve: (parent, args) => {
return _.find(movies, { directorId: parent.id });
},
},
}),
});
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
movie: {
type: MovieType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
console.log(args)
return _.find(movies, { id: args.id });
},
},
director: {
type: DirectorType,
args: {id: {type: GraphQLID}},
resolve(parent, args) {
return _.find(directors, {id: args.id})
}
},
movies: {
type: new GraphQLList(MovieType),
resolve() {
return movies;
},
},
directors: {
type: new GraphQLList(DirectorType),
resolve() {
return directors;
},
},
},
});
module.exports = new GraphQLSchema({
query: RootQuery,
});
not sure what I have missed.

GraphQL: How to make nested queries?

I have defined two independent queries called nextgamedaylf1 and teamclub. Both work fine isolated, but I want to include teamclub query inside of nextgamedaylf1 query.
To do that, I have created a field in the schema of nextgamedaylf1 called home_team which is from type teamclub.
import {
GraphQLObjectType,
GraphQLNonNull,
GraphQLID,
GraphQLInt,
GraphQLBoolean
} from "graphql";
import {DateTime} from "../scalar/dateTime";
import {TeamClub} from "./teamclub.schema";
const NextGameday = new GraphQLObjectType({
name: "NextGameday",
description: "List of games of next gameday",
fields: () => {
return {
id: {
type: new GraphQLNonNull(GraphQLID),
description: "ID of NextGameday",
resolve(ng){
return ng.id
}
},
id_competition: {
type: new GraphQLNonNull(GraphQLInt),
description: "ID of competition",
resolve(ng){
return ng.id_competition
}
},
id_home_team: {
type: new GraphQLNonNull(GraphQLInt),
description: "ID of home team",
resolve(ng){
return ng.id_home_team
}
},
home_team: {
type: TeamClub
},
id_away_team: {
type: new GraphQLNonNull(GraphQLInt),
description: "ID of away team",
resolve(ng){
return ng.id_away_team
}
},
date: {
type: new GraphQLNonNull(DateTime),
description: "Date time of the game",
resolve(ng){
return ng.date
}
},
played: {
type: new GraphQLNonNull(GraphQLBoolean),
description: "Game played. 0: Game NOT played. 1: Game played.",
resolve(ng){
return ng.played
}
}
}
}
});
module.exports.NextGameday = NextGameday;
And when I try to get the data from GraphiQL I've got this error:
So, how can I make a nested query with GraphQL?
Schema for teamclub:
import {
GraphQLObjectType,
GraphQLNonNull,
GraphQLID,
GraphQLInt,
GraphQLString
} from "graphql";
import {DateTime} from "../scalar/dateTime";
const TeamClub = new GraphQLObjectType({
name: "TeamClub",
description: "Data from a team which belongs to a club",
fields: () => {
return {
id: {
type: new GraphQLNonNull(GraphQLID),
description: "ID of team club",
resolve(tc){
return tc.id
},
},
id_league: {
type: new GraphQLNonNull(GraphQLInt),
description: "ID of league",
resolve(tc){
return tc.id_league
}
},
id_season: {
type: new GraphQLNonNull(GraphQLInt),
description: "ID of season",
resolve(tc){
return tc.id_season
}
},
id_club: {
type: new GraphQLNonNull(GraphQLInt),
description: "ID of Club",
resolve(tc){
return tc.id_club
}
},
id_team_feb: {
type: new GraphQLNonNull(GraphQLInt),
description: "ID of FEB",
resolve(tc){
return tc.id_team_feb
}
},
logo: {
type: GraphQLString,
description: "url to the logo",
resolve(tc){
return tc.logo
}
},
name: {
type: new GraphQLNonNull(GraphQLString),
description: "Name of the team which belongs to a club",
resolve(tc){
return tc.name
}
},
abrev: {
type: GraphQLString,
description: "Abreviature of the name of the team",
resolve(tc){
return tc.abrev
}
},
url_name: {
type: new GraphQLNonNull(GraphQLString),
description: "Name for the url of the team",
resolve(tc){
return tc.url_name
}
},
r_name: {
type: GraphQLString,
description: "Name of the team for R graphics",
resolve(tc){
return tc.r_name
}
},
date_start: {
type: new GraphQLNonNull(DateTime),
description: "Date of insert in database",
resolve(tc){
return tc.date_start
}
}
}
}
});
module.exports.TeamClub = TeamClub;

Unknown column 'getContent.movie_id' in 'field list

my whole schema
const Films = new GraphQLObjectType({
name: 'films',
interfaces: () => [MovieStream],
fields: () => ({
movie_id: {
type: GraphQLString,
},
id:{
type: GraphQLID
},
name: {
type: GraphQLString,
},
})
})
Films._typeConfig = {
sqlTable: "films",
uniqueKey: 'id',
}
const MovieStream = new GraphQLInterfaceType({
name: 'MovieStream',
fields: () => ({
id: {
type: GraphQLID,
},
movie_id: {
type: GraphQLString,
},
})
})
MovieStream._typeConfig = {
sqlTable: "movie_streams",
uniqueKey: 'id'
}
const QueryRoot = new GraphQLObjectType({
name: 'Query',
fields: () => ({
getContentList:{
type: new GraphQLList(Films),
args: {
id: {
type: GraphQLInt
},
permalink: {
type: GraphQLString
},
language: {
type: GraphQLString
},
content_types_id: {
type: GraphQLString
},
oauth_token:{
type: GraphQLString
}
},
resolve: (parent, args, context, resolveInfo) => {
return joinMonster.default(resolveInfo,{}, sql => {
return FilmDb.query(sql).then(function(result) {
return result[0];
});
} ,{dialect: 'mysql'});
},
}
})
})
module.exports = new GraphQLSchema({
query: QueryRoot
})
I have again modified my code still got the error
{
"errors": [
{
"message": "Unknown column 'getContent.movie_id' in 'field list'",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"getContentList"
]
}
],
"data": {
"getContentList": null
}
}
My previous post Is it possible to fetch data from multiple tables using GraphQLList
Please check and tell me where i am wrong??? I have already add the field still it does not access the field of that object type

Is left join only one way to fetch data using join monster?

const QueryRoot = new GraphQLObjectType({
name: 'Query',
fields: () => ({
getContentDetails: {
type: new GraphQLList(Films),
args: {
id: {
type: GraphQLInt
},
permalink: {
type: GraphQLString
},
studio_id: {
type: GraphQLString
},
content_types_id: {
type: GraphQLString
}
},
where: (filmTable, args, context) => {
return ` ${filmTable}.permalink = "${args.permalink}" and ${filmTable}.content_types_id = "${args.content_types_id}"`
},
resolve: (parent, args, context, resolveInfo) => {
return joinMonster.default(resolveInfo, {}, sql => {
return FilmDb.query(sql).then(function(result) {
return result[0];
});
},{dialect: 'mysql'});
}
}
})
})

graphql mutation & couchbase

I'm starting working with graphQL with couchbase, and I have a small problem when I run a mutation, what I'm doing is save 2 object:
key: email#email.it
{
type: 'credential',
user_id: 'xxxx-xxxx-xxxx-xxxx',
password: 'jknelkjf'
}
key: xxxx-xxxx-xxxx-xxxx
{
type: 'user',
name: 'aaaa',
surname: 'bbbb'
}
this is my mutation function:
export default {
createUser: {
type: User,
args: {
email: { type: GraphQLString },
password: { type: GraphQLString },
name: { type: GraphQLString },
surname: { type: GraphQLString }
},
resolve(source, args, req) {
const _id = uuid();
return new Promise((resolve, reject) => {
bcrypt.hash(args.password, 10)
.then(hash => req.db.insert({}, args.email, {
password: hash,
user_id: _id,
type: "credential"
}))
.then(cred => req.db.insert({}, cred.user_id, {
name: args.name,
surname: args.surname,
type: "user"
}))
.then(user => {
return resolve(user);
})
.catch(err => {
return reject(err);
});
});
}
}
}
and these are my schemas
export const UserCredential = new GraphQLObjectType({
name: 'UserCredential',
fields: () => ({
user_id: { type: GraphQLString },
type: { type: GraphQLString },
id: { type: GraphQLString },
password: { type: GraphQLString }
})
});
export const User = new GraphQLObjectType({
name: 'User',
fields: () => ({
name: { type: GraphQLString },
surname: { type: GraphQLString },
type: { type: GraphQLString },
id: { type: GraphQLString },
credential: {
type: UserCredential,
resolve: (root, args, req) => {
const query = `SELECT META(credential).id,credential.* FROM {{bucket}} as credential WHERE credential.type="credential" AND credential.user_id=$1`;
return new Promise((resolve, reject) => {
req.db.runQuery(null, query, [root.id])
.then((res) => {
return resolve(res[0]);
})
.catch((err) => {
return reject(err);
})
});
}
}
})
});
the problem is that when I run the mutation the result is the user object with the credential field null, but if I add a timeout on the resolve of the mutation I get the user with the credential object...the insert function is something like this:
db.insert(result => {
return db.find(result.id)
})
do you have any advice?

Resources