Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const graphql = require('graphql');
- const axios = require('axios');
- const {
- GraphQLObjectType,
- GraphQLString,
- GraphQLInt,
- GraphQLSchema,
- GraphQLList,
- GraphQLNonNull,
- GraphQLBoolean
- } = graphql;
- const CompanyType = new GraphQLObjectType({
- name: 'Company',
- fields: () => ({
- id: { type: GraphQLString },
- name: { type: GraphQLString },
- description: { type: GraphQLString },
- users: {
- type: new GraphQLList(UserType),
- resolve(parentValue, args) {
- return axios.get(`http://localhost:3000/companies/${parentValue.id}/users`)
- .then(res => res.data)
- }
- }
- })
- });
- const UserType = new GraphQLObjectType({
- name: 'User',
- fields: () => ({
- 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);
- }
- }
- })
- });
- const RepoType = new GraphQLObjectType({
- name: 'Repo',
- fields: () => ({
- total_count: { type: GraphQLInt },
- incomplete_results: { type: GraphQLBoolean },
- // items: {type: [RepoItemType]}
- items: {
- type: new GraphQLList(RepoItemType),
- resolve(parentValue, args) {
- //console.log(parentValue.items)
- return parentValue.items
- }
- },
- })
- });
- const RepoItemType = new GraphQLObjectType({
- name: 'Item',
- fields: () => ({
- id: { type: GraphQLInt },
- html_url: { type: GraphQLString },
- description: { type: GraphQLString },
- owner: {
- type: OwnerType,
- resolve(parentValue, args) {
- //console.log(parentValue.items)
- return parentValue.owner
- }
- },
- stats: {
- type: new GraphQLList(StateType),
- resolve(parentValue, args) {
- return axios.get(`https://api.github.com/repos/${parentValue.owner.login}/${parentValue.name}/stats/contributors`)
- .then(res => res.data)
- }}
- })
- });
- const StateType = new GraphQLObjectType({
- name: 'State',
- fields: () => ({
- stat: { type: new GraphQLList(StateObj) }
- })
- });
- const StateObj = new GraphQLObjectType({
- name: 'StateObj',
- fields: () => ({
- total: {type: GraphQLInt},
- weeks: {type: new GraphQLList(WeekArray)}
- })
- });
- const WeekArray = new GraphQLObjectType({
- name: 'WeekArray',
- fields: () => ({
- w: {type: GraphQLInt}
- })
- });
- const OwnerType = new GraphQLObjectType({
- name: 'Owner',
- fields: () => ({
- id: { type: GraphQLInt },
- login: { type: GraphQLString },
- })
- });
- const RootQuery = new GraphQLObjectType({
- name: 'RootQueryType',
- fields: {
- repo: {
- type: RepoType,
- args: { q: { type: GraphQLString }, sort: {type: GraphQLString}, order: {type: GraphQLString} },
- resolve(parentValue, args) {
- return axios.get(`https://api.github.com/search/repositories?q=${args.q}&sort=${args.sort}&order=${args.order}`)
- .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);
- // }
- // }
- }
- });
- const mutation = new GraphQLObjectType({
- name: 'Mutation',
- fields: {
- addUser: {
- type: UserType,
- args: {
- firstName: { type: new GraphQLNonNull(GraphQLString) },
- age: { type: GraphQLInt },
- companyId: { type: GraphQLString }
- },
- resolve(parentValue, { firstName, age, companyId }) {
- return axios.post('http://localhost:3000/users', { firstName, age, companyId })
- .then(res => res.data);
- }
- },
- deleteUser: {
- type: UserType,
- args: {
- id: { type: new GraphQLNonNull(GraphQLString) }
- },
- resolve(parentValue, { id }) {
- return axios.delete(`http://localhost:3000/users/${id}`)
- .then(res => res.data);
- }
- }
- }
- });
- module.exports = new GraphQLSchema({
- mutation,
- query: RootQuery
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement