GraphQL学习笔记2

2. 数据查询

2.1 嵌套查询


const CompanyType = new GraphQLObjectType({
    name: 'Company',
    fields: {
        id: {type: GraphQLString},
        name: {type: GraphQLString},
        description: {type: GraphQLString}
    }
});


const UserType = new GraphQLObjectType({
    name: 'User', // name
    fields: {   // properties
        id: {type: GraphQLString},
        firstName: {type: GraphQLString},
        age: {type: GraphQLInt},
        company:{type: CompanyType}
    }
});

One user  --> one company    one company --> many user 

2.2 嵌套查询2

company:{type: CompanyType}

GraphQL学习笔记2_第1张图片
resolve 的作用就是,companyId和company关联

const CompanyType = new GraphQLObjectType({
    name: 'Company',
    fields: {
        id: {type: GraphQLString},
        name: {type: GraphQLString},
        description: {type: GraphQLString}
    }
});


const UserType = new GraphQLObjectType({
    name: 'User', // name
    fields: {   // properties
        id: {type: GraphQLString},
        firstName: {type: GraphQLString},
        age: {type: GraphQLInt},
        company: {type: CompanyType, resolve(parentValue, args){
                return axios.get(`http://localhost:3000/companies/${parentValue.companyId}`)
                    .then(res => res.data)
            }}
    }
});
{
  user(id: "23"){ 
    firstName,
    age,
    company{
      id,
      name,
      description
    }
  }
}

2.3 理解查询过程

GraphQL学习笔记2_第2张图片
user 通过root query找出user, user通过resolve找到company,

2.4 多个root查询

GraphQL学习笔记2_第3张图片

const RootQuery = new GraphQLObjectType({
    name: "RootQueryType",
    fields: {
        user: {
            type: UserType,
            args: {id: {type: GraphQLString}},
            resolve(parentValue, args) {
                return axios.get(`http://localhost:3000/users/${args.id}`)
                    .then(resp => resp.data)
            }
        },
        company: {
            type: CompanyType,
            args: {id: {type: GraphQLString}},
            resolve(parentValue, args){
                return axios.get(`http://localhost:3000/companies/${args.id}`)
                    .then(resp => resp.data)
            }
        }
    }
});

2.5 双向查询

GraphQL学习笔记2_第4张图片

GraphQL学习笔记2_第5张图片1对多

GraphQL学习笔记2_第6张图片

GraphQL学习笔记2_第7张图片

2.6 Query Fragment

Fragement,可以复用的

query findCompany {
	company(id: "1"){
  	id,
    name
  }
}
query findCompany {
	apple:company(id: "1"){
  	id,
    name
  }
  
  google:company(id: "1"){
  	id,
    name
  }
}


GraphQL学习笔记2_第8张图片
GraphQL学习笔记2_第9张图片

query fragment可以复用

2.7 Mutation简介

GraphQL学习笔记2_第10张图片

const mutation = new GraphQLObjectType({
    name: "Mutation",
    fields: {
        addUser: {
            type: UserType, // not aways to return
            args: {
                firstName: {type:GraphQLString},
                age: {type: GraphQLInt},
                companyId: {type:GraphQLString}
            },
            resolve(parentValue, args){
                
            }
        }
    }
});

你可能感兴趣的:(GraphQL)