types.json:
{
"WorkerId": {
"_enum": {
"Single": "Single",
"Double": "Double"
}
},
"Single": "u8",
"Double": "(u8, u8)",
}
substrate code:
#[pallet::storage]
#[pallet::getter(fn worker_infos)]
pub type WorkerInfos<T: Config> = StorageMap<_, Twox64Concat, WorkerId, WorkerInfo, ValueQuery>;
pub enum WorkerId {
Single(u8),
Double(u8, u8),
}
I want to query worker_infos by WorkerId in polkadot.js:
workerIds = [1,2]
api.query[wrpc][wcallable]
.multi(workerIds, (results) => {
...
})
.then((unsub) => {
...
})
.catch(console.error);
Error info:
REGISTRY: Error: Unable to create Enum via index 2, in Single, Double
Any ideas on this? How to pass workerIds(enum type) in polkadot.js?
{ Single: 1 } or { Double: [2, 3] }
Related
I am working on a project in which I receive payload data in the following format:
"name":"John",
"date":"2022-04-14",
"price":200,
"dependencies":[
{
"element_1":{
"items":[2,4],
"pricing":[1,2]
}
},
{
"element_2":{
"items":[5,4],
"pricing":[1,6]
}
}
]
How should I define the struct for this? If you know please let me know. Thanks!
This part
"dependencies":[
{
"element_1":{
"items":[2,4],
"pricing":[1,2]
}
},
{
"element_2":{
"items":[5,4],
"pricing":[1,6]
}
}
]
Looks like
type XXX struct {
Dependencies []map[string]Dependency `json:"dependencies"`
}
type Dependency struct {
Items []int // json…
Pricing []int // json…
}
But I am not sure… if element_x is dynamic or not. If it is predefined, ok.
If your json is
{
"name": "John",
"date": "2022-04-14",
"price": 200,
"dependencies":
[
{
"element_1":
{
"items":
[
2,
4
],
"pricing":
[
1,
2
]
}
},
{
"element_2":
{
"items":
[
5,
4
],
"pricing":
[
1,
6
]
}
}
]
}
Then you can create struct like this
type DemoStruct struct {
Name string `json:"name"`
Date string `json:"date"`
Price int64 `json:"price"`
Dependencies []Dependency `json:"dependencies"`
}
type Dependency struct {
Element1 *Element `json:"element_1,omitempty"`
Element2 *Element `json:"element_2,omitempty"`
}
type Element struct {
Items []int64 `json:"items"`
Pricing []int64 `json:"pricing"`
}
Bear with me, I will explain this the best I can. Please let me know if more information is needed, I am trying to keep this as brief as possible.
I am using Apollo Server and the 'apollo-datasource-rest' plugin to access a REST API. When attempting to get the property values from a nested array of objects I get a null response for each field/property. In addition, the array being queried is only showing a single iteration when multiple are available.
The field in question is the 'cores' field within the Rocket type, i.e., launch.rocket.firstStage.cores
I have attempted various ways of mapping through 'cores' (thinking this was what it wanted) with no success.
To keep things short and simple I'm only including the code for the specific issue. All other parts of the query are operating as expected.
You can view the API response I am hitting here: https://api.spacexdata.com/v3/launches/77
schema.js
const { gql } = require('apollo-server');
const typeDefs = gql`
type Query {
singleLaunch(flightNumber: Int!): Launch
}
type Launch {
flightNumber: Int!
rocket: Rocket
}
type Rocket {
firstStage: Cores
}
type Cores {
cores: [CoreFields]
}
type CoreFields {
flight: Int
gridfins: Boolean
legs: Boolean
reused: Boolean
landingType: String
landingVehicle: String
landingSuccess: Boolean
}
`;
module.exports = typeDefs;
Data Source - launch.js
const { RESTDataSource } = require('apollo-datasource-rest');
class LaunchAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'https://api.spacexdata.com/v3/';
}
async getLaunchById({ launchId }) {
const res = await this.get('launches', {
flight_number: launchId,
});
return this.launchReducer(res[0]);
}
launchReducer(launch) {
return {
flightNumber: launch.flight_number || 0,
rocket: {
firstStage: {
cores: [
{
flight: launch.rocket.first_stage.cores.flight,
gridfins: launch.rocket.first_stage.cores.gridfins,
legs: launch.rocket.first_stage.cores.legs,
landingType: launch.rocket.first_stage.cores.landing_type,
landingVehicle: launch.rocket.first_stage.cores.landing_vehicle,
landingSuccess: launch.rocket.first_stage.cores.landing_success,
},
],
},
};
}
}
module.exports = LaunchAPI;
resolvers.js
module.exports = {
Query: {
singleLaunch: (_, { flightNumber }, { dataSources }) =>
dataSources.launchAPI.getLaunchById({ launchId: flightNumber }),
},
};
Query
query GetLaunchById($flightNumber: Int!) {
singleLaunch(flightNumber: $flightNumber) {
flightNumber
rocket {
firstStage {
cores {
flight
gridfins
legs
reused
landingType
landingVehicle
landingSuccess
}
}
}
}
}
Expected Result
{
"data": {
"singleLaunch": {
"flightNumber": 77,
"rocket": {
"firstStage": {
"cores": [
{
"flight": 1,
"gridfins": true,
"legs": true,
"reused": true,
"landingType": "ASDS",
"landingVehicle": "OCISLY",
"landSuccess": true,
},
{
"flight": 1,
"gridfins": true,
"legs": true,
"reused": false,
"landingType": "RTLS",
"landingVehicle": "LZ-1",
"landSuccess": true
},
{
"flight": 1,
"gridfins": true,
"legs": true,
"reused": false,
"landingType": "RTLS",
"landingVehicle": "LZ-2",
"landSuccess": true
},
]
}
},
}
}
}
Actual Result (Through GraphQL Playground)
{
"data": {
"singleLaunch": {
"flightNumber": 77,
"rocket": {
"firstStage": {
"cores": [
{
"flight": null,
"gridfins": null,
"legs": null,
"reused": null,
"landingType": null,
"landingVehicle": null,
"landingSuccess": null
}
]
}
},
}
}
}
Any suggestions as to what I am doing wrong here would be greatly appreciated. Again, let me know if more information is needed.
Thank you!
Missing base url
There should be
await this.get( this.baseURL + 'launches'
IMHO there should be a map used within launchReducer to return an array, sth like:
launchReducer(launch) {
return {
flightNumber: launch.flight_number || 0,
rocket: {
firstStage: {
cores: launch.rocket.first_stage.cores.map(core => ({
flight: core.flight,
gridfins: core.gridfins,
legs: core.legs,
landingType: core.landing_type,
landingVehicle: core.landing_vehicle,
landSuccess: core.land_success,
})),
},
},
};
}
.map(core => ({ is for returning object [literal], the same as/shorter version of .map(core => { return {
I've a situation like this...
const INITIAL_STATE = {
chat: []
}
Then I set the chat and I include this data:
[
{
"otherParty":"aaaaa",
"thread":[
{
"a":1,
"b":2,
"c":3
},
{
"d":4,
"e":5,
"f":6
}
]
},
{
"otherParty":"bbbb",
"thread":[
{
"a":1,
"b":2,
"c":3
},
{
"d":4,
"e":5,
"f":6
}
]
},
{
"otherParty":"cccc",
"thread":[
{
"a":1,
"b":2,
"c":3
},
{
"d":4,
"e":5,
"f":6
}
]
}
]
I need to add a new item at array[1].thread something like { g: 7, h: 8, i: 9 } - In other words: I'd like to specify the index of the array and add a new thread.
How to archive this ?
export const addNewThread = (obj, index) => {
return {
type: ADD_NEW_THREAD,
payload: {
thread: obj,
index: index
}
}
}
and the reducer...(I need to fill the ????)
const INITIAL_STATE = {
chat: []
}
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case ADD_NEW_THREAD:
return {
...state,
chat: ?????
}
}
return state
}
Something like this
const INITIAL_STATE = {
chat: []
}
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case ADD_NEW_THREAD:
const chat = state.chat.slice();
const thread = chat[action.index].thread.concat(action.thread);
chat.splice(action.index, 1, thread);
return {
chat
};
}
return state
}
with appolo server 2 beta,
I have a resolver like:
Travaux: new GraphQLEnumType({
name: 'Travaux',
values: {
ins: { value: 'en instruction' },
val: { value: 'valide' },
ech: { value: 'échu' }
}
})
and a Schema
gql`
type Query {
titre(id: String!): Titre
}
type Titre {
travaux: Travaux
}
enum Travaux {
ins
val
ech
}
`
This makes an error:
Travaux.name was defined in resolvers, but enum is not in schema
If I remove the Travaux resolver, it works…
What is arong here?
The resolver has to be:
Travaux = {
ins: 'en instruction',
val: 'valide',
ech: 'échu'
}
Im using Apollo 2. With the GraphiQL interface I can run a query which returns all groups fine. However when I try to pass a name to find just 1 group it doenst work.
Schema
type Query {
hi: String
groups: [Group]
group(name: String): Group
}
type Group {
name: String
}
Resolvers:
Query: {
hi() {
return 'howdy';
},
groups() {
return Groups.find().fetch();
},
group(one, two, three) {
console.log('group resolver called');
console.log(one, two, three);
//return Groups.findOne(two.name);
},
},
This is my GraphiQL groups query which works fine:
{
groups {
name
}
}
But my group query returns an error:
{
group(name: "some name") {
name
}
}
{
"errors": [
{
"message": "Unknown argument \"name\" on field \"group\" of type \"Query\".",
"locations": [
{
"line": 2,
"column": 9
}
]
}
]
}
UPDATE - This is my full file:
import { createApolloServer } from 'meteor/apollo';
import { makeExecutableSchema } from 'graphql-tools';
import merge from 'lodash/merge';
import GroupsSchema from '../../api/groups/Groups.graphql';
import GroupsResolvers from '../../api/groups/resolvers';
const typeDefs = [GroupsSchema];
const resolvers = merge(GroupsResolvers);
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
createApolloServer({ schema });
In resolvers.js
import Groups from './groups';
export default {
Query: {
hi() {
return 'howdy';
},
groups() {
return [{ name: '1', test: 'test 1' }, { name: '2', test: 'test 2' }];
// return Groups.find().fetch();
},
group(one, two, three) {
console.log('group resolver called');
console.log(one, two, three);
// return Groups.findOne('Apon9HKE9MeZqeXsZ');
},
},
};
In Groups.graphql
type Query {
hi: String
groups: [Group]
group(name: String): Group
}
type Group {
name: String
}
I think your typeDefs and resolvers are wrongs, try:
typeDefs
const typeDefs = `
type Query {
hi: String
groups: [Group]
group(name: String): Group
}
type Group {
name: String
}`;
Resolvers
export default {
Query: {
hi() {
return 'howdy';
},
groups() {
return [{ name: '1', test: 'test 1' }, { name: '2', test: 'test 2' }];
// return Groups.find().fetch();
},
group(_, { name }) {
console.log('group resolver called');
console.log(one, two, three);
// return Groups.findOne('Apon9HKE9MeZqeXsZ');
},
},
};