frankcollins3 / PHPokedex

react concepts
0 stars 0 forks source link

export func from src/utility to GraphQL server through post body data [8:46pm] #35

Closed frankcollins3 closed 1 year ago

frankcollins3 commented 1 year ago

import axios from 'axios' **here is a /utility function that takes param of key which would be {int:id} || {string:name} ***

export default async function nameOrIdForData(key) {
        if (key) {
            // parameter can be an integer (pokemon.id) or a name (pokemon.name)
// access axios. check for pokeAPI related endpoint data.abilities. and render that data or empty array. 
            let predata = await axios.get(`https://pokeapi.co/api/v2/pokemon/${key}`)
            let data = predata.data.abilities ? predata.data : []            
            return data
        } else {
            return  
        }            
}

here is that function imported and then put as the value for a {getAllData:} key. import nameOrIdForData from '../../utility/nameOrIdForData' import axios from 'axios'

const firefunc = async () => { let myData = await nameOrIdForData(88); // console.log('myData') console.log(myData)

const response = await axios.post('http://localhost:5000/pokemon?query={allDataAllPokemon{name}}', {
  getAllData: **nameOrIdForData**
});    
return response || 'no response'

}

my issue/blocker is now how to send over that function while passing constraint of needing to apply to one of these imports.

const {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLList,
  GraphQLInt,
  GraphQLNonNull
} = require('graphql')

first proposed approach: perhaps GraphQLObjectType would work but if not maybe there is another one of those imports that would allow for that.

another approach is that its not the end of world to hand write that very simple function that returns all data for params in the server so that it is used in the GraphQL route without need to be imported (because it cant its outside of /src) and cant be snuck through a POST data call with the function as post body data as far as I can say.

server side that function would be retrieved from req.body.getAllData if not mistaken

[8:46pm]

frankcollins3 commented 1 year ago

Screen Shot 2023-05-10 at 8 54 50 PM

Screen Shot 2023-05-10 at 8 54 43 PM

handwritten function it is.

[8:55pm]

frankcollins3 commented 1 year ago

used the same function from client/src/utility in /server/index.js. endpoints to access (pokemon.type)// pokemon.types[0||1].type.name is at the /api/pokemon route not /api route (pokeAPI) [10:26am]