Why do we only see the displacement of end points of spring while calculating it's potential energy? I am a high school student and I am a little confused in understanding the spring potential energy formula. When we calculate the spring potential energy of a spring attached to a wall at one end and we say that if we stretch the spring end say up to length $x$ we do work against the restoring conservative force hence we increase the potential energy of spring by $\frac{1}{2} k x^2$ where $k$ is the spring constant and $x$ is the displacement.
The problem is why do we only see the displacement of the end point of the spring? According to this formula this is the potential energy of only the end point because we applied force only at the end point, right?
I am comparing this with say a mass raised up to the height $h$ from the earth surface considering the reference at surface of earth. We see that the negative work done by the gravity is $-mgh$ which increases the potential energy of the mass. Why don't we see the displacement of each point and add them up to get the total energy of stretched or compressed spring?
If we do this my answer comes out to be $\frac{d}{L} \frac{1}{2} k x^2$ where $d$ is the point for which we have to see the displacement against its restoring conservative force with reference to the fixed end and $L$ is the which is equal i.e $kx$ for all the points as spring is massless so at any point the net force at any point should be 0 otherwise there would be infinite acceleration, see that at the end point i.e length $L$ the work done against conservative restoring force is $\frac{1}{2} k x^2$. So what's the problem with my understanding, why do we only see the displacement of end points or how much the total spring is stretched from its relaxed position?
 A: For simplicity, let’s consider a massless spring.

According to this formula this is the potential energy of only the end point because we applied force only at the end point, isn't it?

What would have happened if you pull the spring when its other end is free? The whole spring would start moving in the direction of the pull and would not elongate at all. This is in line with the second Newton's law of motion
$$m \vec{a} = \sum_i \vec{F}_i$$
where $m$ is mass of the spring, $\vec{a}$ is acceleration, and $\sum_i \vec{F}_i$ is sum of all forces acting on the spring.
If the string is attached to the wall, then the wall exerts force on the spring which is of the same magnitude and opposite in direction to the force you exert on the other end. In this case the spring does not move but rather elongates.

I am comparing it with say a mass raised upto height "h" from the earth surface considering the reference at surface of earth we see that the negative work done by the gravity is "-mgh" which increases the potential energy of the mass.

Correct. Just like gravitational force does negative work on an object, so does the spring's restoring force when you elongate or compress the spring.

So, Why we don't see the displacement of each point and add them up to get the total energy of stretched or compressed spring?

Because gravitational force and spring restoring force are not the same thing, they are defined differently
$$F_g = m g \qquad \text{and} \qquad F_e = k x$$
In other words, the gravitational force magnitude does not depend on height* while the spring restoring force does depend on the elongation (position). It is true that when you elongate the spring upwards you are also increasing the spring's gravitational potential energy, but since the spring is considered to be massless this is negligible.
In reality, the gravitational force does depend on height. The expression for the gravitational potential energy $U_g = mgh$ works only close to the surface of Earth.
