-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] GraphQL .

, 11 2017 . 12:47 +

! GraphQL, . , . , .

GraphQL, . .





, , . .



, Queries( ) Mutations( ).

Queries


user , : firstName lastName.

query myQuery{
    user {
       firstName
       lastName
   }
}

GraphiQL, query . , , , .

Mutations


, :

mutation myMutation{
  UserCreate(data: {firstName: "Jane", lastName: "Dohe"})
}

Queries , mutations .

Subsciptions


GraphQL subscriptions().

subscriptions mySubscription{
    user {
       firstName
       lastName
   }
}

queries queries, subscriptions.

, .


. queries, mutation subscription.

, . , .

query myQuery($id: '1'){
    user (id:$id){
       firstName
       lastName
   }
}

id = '1' firstName lastName.


, . GraphiQL , .

query myQuery($id: ID){ // 
    user (id:$id){
       firstName
       lastName
   }
}

{ 
  "id": "595fdbe2cc86ed070ce1da52"  // 
}

, :



ID! GraphQL, .

, , . JS : vue-apollo, react-apollo, apollo-angular.


, ( ). MongoDB:

// MongoDB schema
const schema = new mongoose.Schema({
    firstName: {
        type: String
    },
    lastName: {
        type: String
    },
})

export const USER_MODEL = mongoose.model('users', schema)

// GraphQL type
const user = {
    firstName: {
        type: GraphQLString
    },
    lastName: {
        type: GraphQLString
    },
}

export const USER = new GraphQLObjectType({
    name: 'USER',
    fields: user
})

:

// MongoDB schema
const schema = new mongoose.Schema({
    firstName: {
        type: String
    },
    lastName: {
        type: String
    },
    secure: {
        public_key: {
            type: String
        },
        private_key: {
            type: String
        },
    },
})

. . .

const secure = new GraphQLObjectType({
    name: "public_key",
    fields: {
        public_key: {
            type: GraphQLString
        }
    }
})

export const USER = new GraphQLObjectType({
    name: "USER",
    fields: {
        firstName: {
            type: GraphQLID
        },
        secure: {
            type: secure
        }
    }
})

Query :

query myQuery($id: ID){ 
    user (id:$id){ 
       firstName
       secure {
          public_key
       }
   }
}


Mutations + Queries


true/false. GraphQLBolean , :

mutation auth($email: String!, $password: String!) {
  auth(email: $email, password: $password) {
    id
    secure {
      public_key
    }
  }
}




, . , , .

.
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/332850/

:  

: [1] []
 

:
: 

: ( )

:

  URL