首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >灯塔GraphQL将ID转换为字符串而不是整数?

灯塔GraphQL将ID转换为字符串而不是整数?
EN

Stack Overflow用户
提问于 2020-12-09 00:04:28
回答 1查看 973关注 0票数 2

我有一个应用程序,在后端使用Laravel和灯塔,在前端使用Nuxt和Vuex-ORM/graphql插件。我很难进行比较,因为无论出于什么原因,有时数据会被查询或变异,而is似乎是整数或字符串的混合体。

我正在努力确保每个ID实际上都是一个整数。每当我在前面的console.log旅行,该旅行的ID仍然是一个字符串。当我在graphql-游乐场中运行查询时,它作为字符串返回,但我认为这更多是因为它是一个"ID“标量类型,可以是int或字符串。或者别的什么。我不知道,这件事我还是很不高兴。

Trip.php:

代码语言:javascript
复制
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Trip extends Model
{
    protected $fillable = [
        'name', 'description', 'user_id'
    ];

    // automatically eager load todos and shopping_list_items
    protected $with = [
        'items', 'users'
    ];

    // define relationships
    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class);
    }
}

trip.graphql:

代码语言:javascript
复制
extend type Query @guard(with: ["api"]) {
    trip(id: ID! @eq): Trip! @find
    trips: [Trip]! @field(resolver: "TripsQuery@find_by_user")
}

extend type Mutation @guard(with: ["api"]) {
    updateTrip(id: ID! trip: TripInput! @spread): Trip @update
}

type Trip {
    id: ID!
    name: String
    description: String
    created_at: DateTime!
    updated_at: DateTime!
    user_id: ID

    items: [Item]! @hasMany
    users: [User]! @hasMany
}

input TripInput {
    name: String
    description: String
    user_id: ID
}

trip.js:

代码语言:javascript
复制
import { Model } from '@vuex-orm/core';
import Item from './item';
import TripUser from './tripUser';
import User from './user';

export default class Trip extends Model {
  static entity = 'trips';
  static eagerLoad = ['items', 'users'];

  static fields () {
    return {
      id: this.number(0),
      name: this.string(''),
      description: this.string(''),
      user_id: this.number(0),

      // relationships
      items: this.hasMany(Item, 'trip_id'),
      users: this.belongsToMany(User, TripUser, 'trip_id', 'user_id')
    };
  }
};

planning.vue:

代码语言:javascript
复制
<script>
  import currentUser from '~/mixins/currentUser';
  import Trip from '~/data/models/trip';
  import tripsQuery from '~/apollo/queries/content/trips.gql';

  export default {
    name: 'Planning',

    middleware: 'authenticated',

    mixins: [currentUser],

    data: () => ({
      loading: 1,
    }),

    computed: {
      trips () {
        return Trip.query().where('owner_id', this.currentUser.id).all();
      }
    },

    async mounted () {
      const { trips } = await this.$store.dispatch('entities/simpleQuery', {
        query: tripsQuery,
        variables: {},
        bypassCache: false
      });
      console.log(typeof trips[0].id); // <--- returns 'string';
      Trip.insert({ data: [...trips] });
      this.selectedTrip = trips.length ? trips[0] : null;
      this.loading = 0;
    },

    head () {
      return {
        title: 'Planning'
      };
    }
  };
</script>

您可以在Vue文件中的console.log函数mounted中看到,id仍然返回为字符串而不是整数。我如何确保它总是一个整数呢?

更新:我认为我使用的@vuex-orm/库将范围缩小到了一个更大的问题。我试图稍微恢复一下,这样所有的东西都使用字符串id,但是我发现,当我发送一个突变时,由于某种原因,会有将id转换为整数的代码。我将向那个库提交一个错误报告,并结束这个问题。

EN

回答 1

Stack Overflow用户

发布于 2020-12-09 01:39:23

我想在这里插入,并指出ID是按设计转换为字符串,而不是作为bug。你看过更多关于它的文章,所以答案是:https://stackoverflow.com/a/47888604/11678503

简而言之,ID是在GraphQL规范 (2018年6月工作规范)中定义的标量类型:

ID标量类型表示唯一标识符,通常用于重新获取对象或作为缓存的键。ID类型是以与字符串相同的方式序列化的;但是,它不打算是人类可读的。虽然它通常是数字的,但它应该总是以字符串的形式序列化。

虽然数据库中的ID通常是一个自动递增的数字键,但这绝不是一个标准。许多API的使用哈希和UUID,因为它取决于用例。因此,ID可以始终是字符串,但它不能总是数字-因此,为什么转换为字符串是一个更安全的赌注。

因此,我希望这能澄清为什么灯塔,以及graphql-php库将ID转换为字符串的原因。他们遵守了规范。

希望这能帮你把事情弄清楚!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65208770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档