我如何在graphQL突变中修复柴油和杜松的sql类型错误?

问题描述

当我尝试通过柴油用graphGL和MysqL创建突变时,我遇到了以下错误日志。

目前,枚举的类型只是柴油类型,但我想用graphQl的类型来实现。 我为graphQL实现了客户结构,如下所示。 还不够吗?您是否有解决的办法? 谢谢

错误日志

error[E0277]: the trait bound `graphql::Customer: diesel::Queryable<(diesel::sql_types::Unsigned<diesel::sql_types::BigInt>,diesel::sql_types::Text,diesel::sql_types::Timestamp,diesel::sql_types::Timestamp),_>` is not satisfied
  --> src/graphql.rs:60:14
   |
60 |             .first::<crate::graphql::Customer>(&executor.context().db_con)
   |              ^^^^^ the trait `diesel::Queryable<(diesel::sql_types::Unsigned<diesel::sql_types::BigInt>,_>` is not implemented for `graphql::Customer`
   |
   = note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<_,graphql::Customer>` for `diesel::query_builder::SelectStatement<schema::customers::table,diesel::query_builder::select_clause::DefaultSelectClause,diesel::query_builder::distinct_clause::NodistinctClause,diesel::query_builder::where_clause::NowhereClause,diesel::query_builder::order_clause::NoOrderClause,diesel::query_builder::limit_clause::LimitClause<diesel::expression::bound::Bound<diesel::sql_types::BigInt,i64>>>`

src / graphql.rs

use std::convert::From;
use std::sync::Arc;
use chrono::NaiveDateTime; 
use actix_web::{web,Error,HttpResponse};
use futures01::future::Future;

use juniper::http::playground::playground_source;
use juniper::{http::GraphQLRequest,Executor,FieldResult,FieldError,ID};
use juniper_from_schema::graphql_schema_from_file;

use diesel::prelude::*;

use itertools::Itertools;
use crate::schema::customers;

use crate::{DbCon,DbPool};

graphql_schema_from_file!("src/schema.graphql");

pub struct Context {
    db_con: DbCon,}
impl juniper::Context for Context {}

pub struct Query;
pub struct Mutation;

impl QueryFields for Query {
    fn field_customers(
        &self,executor: &Executor<'_,Context>,_trail: &QueryTrail<'_,Customer,Walked>,) -> FieldResult<Vec<Customer>> {
        //type FieldResult<T> = Result<T,String>;

        customers::table
            .load::<crate::models::Customer>(&executor.context().db_con)
            .and_then(|customers| Ok(customers.into_iter().map_into().collect()))
            .map_err(Into::into)
    }
}

impl MutationFields for Mutation {
    fn field_create_customer(
        &self,name: String,email: String,) -> FieldResult<Customer> {
        //type FieldResult<T> = Result<T,String>;

        let new_customer = crate::models::NewCustomer { name: name,email: email};

        diesel::insert_into(customers::table)
            .values(&new_customer)
            .execute(&executor.context().db_con);
            
        customers::table
            .first::<crate::graphql::Customer>(&executor.context().db_con)
            .map_err(Into::into)
    }
}

pub struct Customer {
    id: u64,created_at: NaiveDateTime,updated_at: NaiveDateTime,}

impl CustomerFields for Customer {
    fn field_id(&self,_: &Executor<'_,Context>) -> FieldResult<juniper::ID> {
        Ok(juniper::ID::new(self.id.to_string()))
    }

    fn field_name(&self,Context>) -> FieldResult<&String> {
        Ok(&self.name)
    }
    fn field_email(&self,Context>) -> FieldResult<&String> {
        Ok(&self.email)
    }
}

impl From<crate::models::Customer> for Customer {
    fn from(customer: crate::models::Customer) -> Self {
        Self {
            id: customer.id,name: customer.name,email: customer.email,created_at: customer.created_at,updated_at: customer.updated_at,}
    }
}

fn playground() -> HttpResponse {
    let html = playground_source("");
    HttpResponse::Ok()
        .content_type("text/html; charset=utf-8")
        .body(html)
}

fn graphql(
    schema: web::Data<Arc<Schema>>,data: web::Json<GraphQLRequest>,db_pool: web::Data<DbPool>,) -> impl Future<Item = HttpResponse,Error = Error> {
    let ctx = Context {
        db_con: db_pool.get().unwrap(),};

    web::block(move || {
        let res = data.execute(&schema,&ctx);
        Ok::<_,serde_json::error::Error>(serde_json::to_string(&res)?)
    })
    .map_err(Error::from)
    .and_then(|customer| {
        Ok(HttpResponse::Ok()
            .content_type("application/json")
            .body(customer))
    })
}

pub fn register(config: &mut web::ServiceConfig) {
    let schema = std::sync::Arc::new(Schema::new(Query,Mutation));

    config
        .data(schema)
        .route("/",web::post().to_async(graphql))
        .route("/",web::get().to(playground));
}

src / models.rs

#[derive(Queryable,Identifiable,AsChangeset,Clone,PartialEq,Debug)]
pub struct Customer {
    pub id: u64,pub name: String,pub email: String,pub created_at: NaiveDateTime,pub updated_at: NaiveDateTime,}

use super::schema::customers;
#[derive(Queryable,Insertable,AsChangeset)]
#[table_name="customers"]
pub struct NewCustomer {
    pub name: String,}

依赖项

[dependencies]
diesel = { version = "1.4.5",features = ["MysqL","r2d2","chrono"] }
dotenv = "~0.15"
serde = "~1.0"
serde_derive = "~1.0"
serde_json = "~1.0"
chrono = "~0.4"
rand = "0.7.3"
actix-web = "1.0.9"
actix-cors = "0.1.0"
juniper = "0.14.1"
juniper-from-schema = "0.5.1"
juniper-eager-loading = "0.5.0"
r2d2_MysqL = "*"
r2d2-diesel = "0.16.0"
MysqL = "*"
r2d2 = "*"
futures01 = "0.1.29"
itertools = "0.8.2"

src / schema.graphql

schema {
  query: Query
  mutation: Mutation
}

type Query {
  customers: [Customer!]! @juniper(ownership: "owned")
}

type Mutation {
  createCustomer(
    name: String!,email: String!,): Customer! @juniper(ownership: "owned")
}

type Customer {
  id: ID! @juniper(ownership: "owned")
  name: String!
  email: String!
}

src / schema.rs

table! {
    customers (id) {
        id -> Unsigned<Bigint>,name -> Varchar,email -> Varchar,created_at -> Timestamp,updated_at -> Timestamp,}
}

解决方法

如错误消息所述,您需要为Queryable中的结构Customer实现src/graphql.rs(该结构位于错误消息指向的下方)。最简单的方法是在此结构中添加一个#[derive(Queryable)]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...