graphql中的关系解析器

时间:2019-03-26 15:04:33

标签: graphql sequelize.js apollo apollo-server

我想执行此请求,以使所有存储桶都具有特定语言:

{
   buckets{
      id,
      code,
      language(id:1){
         code
      }
   }
}

但是我有这个回应:

{
  "data": {
    "buckets": [
      {
        "id": 1,
        "language": {
          "code": "FR"
        }
      },
      {
        "id": 2,
        "language": {
          "code": "FR"
        }
      },
      {
        "id": 3,
        "language": "FR" <= Real value "EN"
      },
      {
        "id": 4,
        "language": "FR" <= Real value "EN"
      }
    ]
  }
}

我只想选择具有语言ID的存储桶:

{
  "data": {
    "buckets": [
      {
        "id": 1,
        "language": {
          "code": "FR"
        }
      },
      {
        "id": 2,
        "language": {
          "code": "FR"
        }
      }
    ]
  }
}

schema.js

import { gql } from 'apollo-server';

export const typeDefs = gql`

  # Language
  type Language {
    id: Int!
    name: String
    code: String
    is_active: Boolean
  }

  # Bucket
  type Bucket {
    id: Int!,
    code: String
    language(id: Int): Language
  }

  # query for types
  type Query {
    buckets: [Bucket]
  }

  schema {
    query: Query
  }
`;

export default typeDefs;

resolvers.js

import GraphQLDate from 'graphql-date';
import { Bucket, Language } from './connectors';

export const resolvers = {
    Date: GraphQLDate,
    Query: {
        buckets(_, args) {
            return Bucket.findAll({
                where: args,
                order: [['created_at', 'DESC']],
            });
        }
    },
    Bucket: {
       language(bucket, {id}) {
           console.log('resolver id', id);
           const where = {id: bucket.fk_language_id };

           if (id) {
               where.id = id
           }

           return Language.findOne({
               where: where,
           });
       },
   }
};

export default resolvers;

connectors.js

...

// define language
const LanguageModel = db.define('language', {
    name     : {type: Sequelize.STRING},
    code     : {type: Sequelize.STRING},
    is_active: {type: Sequelize.BOOLEAN}
});

// define bucket
const BucketModel = db.define('bucket', {
    code     : {type: Sequelize.STRING},
    is_active: {type: Sequelize.BOOLEAN}
});

BucketModel.belongsTo(LanguageModel, {
    foreignKey: 'fk_language_id'
});

LanguageModel.hasMany(BucketModel, {
    foreignKey: 'id'
});

...

我的数据库非常简单,只有两个表Language和Bucket。

数据库

语言

  

id |名称|代码is_active

  

id | fk_language_id |代码created_at | Updated_at

有此请求:

{
buckets{
    id,
  language{
    code
  }
  }
}

我有这个结果:

{
  "data": {
    "buckets": [
      {
        "id": 1,
        "language": {
          "code": "FR"
        }
      },
      {
        "id": 2,
        "language": {
          "code": "FR"
        }
      },
      {
        "id": 3,
        "language": {
          "code": "EN"
        }
      },
      {
        "id": 4,
        "language": {
          "code": "EN"
        }
      }
    ]
  }
}

0 个答案:

没有答案